C#C
C#4y ago
yatta

How to assert HttpResponseMessage content in unit test [Answered]

Let's say I have 2 variable like this:
var result = await sender.SendAsync(request);
var testResMess = new HttpResponseMessage();
testResMess.Content = new StringContent("\"test message\"");

I want to test them in unit test to see if they're equal (which I know they are but I still need to do it in the unit test), so I do something like this:
Assert.AreEqual(testResMess.Content, result.Value);
csharp
but the IDE yields The EqualTo constraint always fails as the actual and the expected value cannot be equal So i tried to change it to
Assert.AreEqual(testResMess.Content.ToString(), result.Value);

And this time, I got the error:

Expected string length 29 but was 12. Strings differ at index 0. Expected: "System.Net.Http.StringContent" But was: "test message" -----------^

At this rate, I have no idea is the problem I got and how I can solve them. I'm also new to C# Unit Testing so any help from expert would be really appriciate.
Was this page helpful?