RPM1984 RPM1984 - 4 months ago 14x
JSON Question

How to unit test an Action method which returns JsonResult?

If I have a controller like this:

public JsonResult FindStuff(string query)
var results = _repo.GetStuff(query);
var jsonResult = results.Select(x => new
id = x.Id,
name = x.Foo,
type = x.Bar

return Json(jsonResult);

Basically, I grab stuff from my repository, then project it into a
of anonymous types.

How can I unit-test it?

has a property called
, but it's of type
, as we expected.

So does that mean if I want to test that the JSON object has the properties I expect ("id", "name", "type"), I have to use reflection?


Here's my test:

// Arrange.
const string autoCompleteQuery = "soho";

// Act.
var actionResult = _controller.FindLocations(autoCompleteQuery);

// Assert.
Assert.IsNotNull(actionResult, "No ActionResult returned from action method.");
dynamic jsonCollection = actionResult.Data;
foreach (dynamic json in jsonCollection)
"JSON record does not contain \"id\" required property.");
"JSON record does not contain \"name\" required property.");
"JSON record does not contain \"type\" required property.");

But I get a runtime error in the loop, stating "object does not contain a definition for id".

When I breakpoint,
is defined as a
of anonymous types, so I figure if I enumerate through these, I can check the properties. Inside the loop, the object does have a property called "id" - so not sure what the issue is.


RPM, you look to be correct. I still have much to learn about dynamic and I cannot get Marc's approach to work either. So here is how I was doing it before. You may find it helpful. I just wrote a simple extension method:

    public static object GetReflectedProperty(this object obj, string propertyName)

        PropertyInfo property = obj.GetType().GetProperty(propertyName);

        if (property == null)
            return null;

        return property.GetValue(obj, null);

Then I just use that to do assertions on my Json data:

        JsonResult result = controller.MyAction(...);
        Assert.That(result.Data, Is.Not.Null, "There should be some data for the JsonResult");
        Assert.That(result.Data.GetReflectedProperty("page"), Is.EqualTo(page));