Say, we have developed a REST API based on .NET Web API framework and need to write a unit test for an end point that returns a collection of objects. This sounds like a usual trivial task that many developers do all the time. Unless, the API method that we need to test, returns a collection of anonymous objects, that is the API method is written using a C# anonymous or dynamic type to construct objects that are included in the list.
It's totally fine when the endpoint is consumed over HTTP as the result is first serialized into JSON for transfer and then de-serialized back into a JavaScript object on a consumer side. If, however, the endpoint is called directly in C# without JSON conversion, like in a unit test, it's an entirely different story. Let's discuss how we can overcome this issue.