Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test an Action method which returns JsonResult?

If I have a controller like this:

[HttpPost] 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    }).ToList();     return Json(jsonResult); } 

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

How can I unit-test it?

System.Web.Mvc.JsonResult has a property called Data, but it's of type object, 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?

EDIT:

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) {    Assert.IsNotNull(json.id,         "JSON record does not contain \"id\" required property.");    Assert.IsNotNull(json.name,         "JSON record does not contain \"name\" required property.");    Assert.IsNotNull(json.type,         "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, actionResult.Data is defined as a List<T> 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.

like image 998
RPM1984 Avatar asked Feb 14 '11 06:02

RPM1984


People also ask

How do I assert JsonResult?

We can return jsonResult via invoking the Josn() method in the controller, in general, I will pass one anonymous object such as Josn(new {isSuccess = true, message="It's success!"}); var actionResult = controller. JosnMethod() as JsonResult; Assert.

What does JsonResult return?

JsonResult is one of the type of MVC action result type which returns the data back to the view or the browser in the form of JSON (JavaScript Object notation format).


1 Answers

I know I'm a bit late on this guys, but I found out why the dynamic solution wasn't working:

JsonResult returns an anonymous object and these are, by default, internal, so they need to be made visible to the tests project.

Open your ASP.NET MVC application project and find AssemblyInfo.cs from folder called Properties. Open AssemblyInfo.cs and add the following line to the end of this file.

[assembly: InternalsVisibleTo("MyProject.Tests")] 

Quoted from: http://weblogs.asp.net/gunnarpeipman/archive/2010/07/24/asp-net-mvc-using-dynamic-type-to-test-controller-actions-returning-jsonresult.aspx

I thought it would be nice to have this one for the record. Works like a charm

like image 128
Sergi Papaseit Avatar answered Nov 05 '22 18:11

Sergi Papaseit