I faced a problem asserting two anonymous types.
Yet again, while trying to test again equality following assertion exceptions occur:
1.
Assert.IsTrue(actionResult.Value.Equals(expectedActionResult.Value));
Expected: True But was: False
2.
Assert.AreEqual(actionResult.Value, expectedActionResult.Value);
Expected: <{ errorCode = -4, errorMessage = Invalid or Missing parameters within the request. }> (<>f__AnonymousType0'2[System.Int32,System.String]) But was: <{ errorCode = -4, errorMessage = Invalid or Missing parameters within the request. }> (<>f__AnonymousType0'2[System.Int32,System.String])
This is where I create real and expected result:
var actionResult = _systemUnderTest.GetToken(null) as JsonResult;
var expectedActionResult =
new JsonResult(new
{
errorCode = (int)ErrorCodes.InvalidOrMissingParameters, errorMessage = ErrorCodes.InvalidOrMissingParameters.GetDescription()
});
What am I missing?
Even though the anonymous types are accessible in your test project, that doesn't mean they'll be used when you write new { ... }
.
If you look at actionResult.Value.GetType()
and expectedActionResult.Value.GetType()
I strongly suspect you'll see that they're different types from different assemblies.
The simplest workaround in this case is probably just to compare the resulting JSON instead.
See more on this question at Stackoverflow