Testing Anonymous Return Types Using ASP.NET MVC Action Method

You might encounter a situation where you want to test the returned value of an ASP.NET MVC controller like this action method:

public JsonResult GetById(long id)
{
var person = personRepository.GetById(id);
return Json(person, JsonRequestBehavior.AllowGet);
}

You can test an action method by invoking the controller’s action method to get the returned result. The actual data of interest will then get wrapped according to its data property.

You can also test for Anonymous types using dynamic types. Anonymous types are internal, so you need to add InternalsVisibleTo attribute on the tested assembly, so be cautious when using Anonymous types.

These tests can be written in a very simple form. Read this blog from DotNetDZone.com to find out.

Leave a Reply

Your email address will not be published. Required fields are marked *