I have to write a unit test that is testing a controller that happens to be returning JSON as an anonymous type.
The only reason this is anonymous is because I need to add a root node to it, so the return from the controller looks like this:
return Json(new { User = person });
This adds a root node to the JSON with "User", followed by a Person
object serialized to JSON.
This works fine, my problem is in the unit test.
The only solution I've seen to testing anonymous types like this is to first make the test project visible using InternalsVisibleTo
in AssemblyInfo.cs and then using dynamic
to get the results.
dynamic results = userController.GetPerson(1);
dynamic content = results.Content;
This fails on the second line, saying that object
does not have a Content property. However, under the debugger, it shows the Content property.
How am I supposed to go about testing this?
Aucun commentaire:
Enregistrer un commentaire