RSS

Asp.net MVC JsonResult, Anonymous types and Testing

11 Feb

This article is an explanation on how to access the data from a JsonResult for using it in your unit testing

The JsonResult type has a property called Data of type Object that you can parse if you have used a concrete object as your data for example:

Model

public class Person
  {
    public int Id { get; set; }
    public string Name { get; set; }
    public string Age { get; set; }
  }

Controller Action

public JsonResult Index()
{
  //Logic ....
  var person = new Person
                      {
                          Id = 1,
                          Name = "Nadeem",
                          Age = "24"
                      };
  return Json(person);
}

Unit Test

[Test]
public void Testing()
{
  var controller = new HomeController();

  var result = controller.Test();
  var data = result.Data as Person; //you can now access the Person properties

  //Assert
}

This is all good but we get stuck if we want to pass an anonymous object to our JsonResult there is no way to cast to something to access an anonymous object properties

the solution is i made two custom helper methods that we can use to get values from a JsonResult.Data object
ill follow after with an example of using it

The custom helper:

public static class Helper
{
  public static object GetValue(this JsonResult result,string propertyName)
  {
    IDictionary<string, object> wrapper = new System.Web.Routing.RouteValueDictionary(result.Data);
    return wrapper[propertyName];
  }
  public static T GetValue<T>(this JsonResult result, string propertyName)
  {
    return (T) GetValue(result, propertyName);
  }
}

Example:

Controller Action

public JsonResult Index()
{
  //Logic ....
  return Json(new {number = 1});
}

Unit Test

[Test]
public void Testing()
{
  var controller = new HomeController();

  var result = controller.Index();

  Assert.AreEqual(1, result.GetValue<int>("number"));
}

This is how to test a JsonResult usingĀ  anonymous types

 

For the fun of it we can make a helper that returns a dynamic type (but its not as efficient as the top solution)

The custom helper:

public static dynamic ToDynamic(this JsonResult jsonResult)
{
  var expandoObject = new ExpandoObject();
  var expandoCollection = (ICollection<KeyValuePair<string, object>>)expandoObject;
  var dataWrapper = new RouteValueDictionary(jsonResult.Data);
  foreach (var kvp in dataWrapper)
  {
    expandoCollection.Add(kvp);
  }
  dynamic eoDynamic = expandoObject;
  return eoDynamic;
}

Unit Test

[Test]
public void Testing()
{
  var controller = new HomeController();

  var result = controller.Index();

  Assert.AreEqual(1, result.ToDynamic().number);
}
About these ads
 
2 Comments

Posted by on February 11, 2013 in MVC

 

Tags: ,

2 Responses to Asp.net MVC JsonResult, Anonymous types and Testing

  1. Emad Ibrahim (@eibrahim)

    February 28, 2013 at 12:15 am

    Can you do this with dynamic types? I mean could you somehow do something like

    result.ToDynamic().number

     
    • Nadeem

      February 28, 2013 at 10:27 am

      Thanks for the tip, I have added a section for how to implement that,but its not as efficient as the previous implementation

       

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: