Thursday, December 16, 2010

Using RouteValueDictionary to test anonymous objects in C#

For example, you have the following anonymous type declaration & instantiation:
var someObject = new {someProperty"someValue"};
How to inspect value of the id property? You need a wrapper, that is able to index anonymous objects. For example, RouteValueDictionary.

Here is code:
var wrapper = new RouteValueDictionary(someObject);
Assert.AreEqual("someValue", wrapper["someProperty"]);

If your ASP.NET MVC controller returns JsonResult, you can use this approcah to test contents of the Data property, which usually references to some anonymous object.

No comments:

Post a Comment