Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asserting JsonResult Containing Anonymous Type

I was trying to unit test a method in one of my Controllers returning a JsonResult. To my surprise the following code didn't work:

[HttpPost] public JsonResult Test() {     return Json(new {Id = 123}); } 

This is how I test it (also note that the test code resides in another assembly):

// Act dynamic jsonResult = testController.Test().Data;  // Assert Assert.AreEqual(123, jsonResult.Id); 

The Assert throws an exception:

'object' does not contain a definition for 'Id'

I've since resolved it by using the following:

[HttpPost] public JsonResult Test() {    dynamic data = new ExpandoObject();    data.Id = 123;    return Json(data); } 

I'm trying to understand why isn't the first one working ? It also seems to be working with basically anything BUT an anonymous type.

like image 317
Dimitar Dimitrov Avatar asked Jun 01 '13 18:06

Dimitar Dimitrov


2 Answers

To be clear, the specific problem you are encountering is that C# dynamic does not work with non-public members. This is by design, presumably to discourage that sort of thing. Since as LukLed stated, anonymous types are public only within the same assembly (or to be more precise, anonymous types are simply marked internal, not public), you are running into this barrier.

Probably the cleanest solution would be for you to use InternalsVisibleTo. It allows you to name another assembly that can access its non-public members. Using it for tests is one of the primary reasons for its existance. In your example, you would place in your primary project's AssemblyInfo.cs the following line:

[assembly: InternalsVisibleTo("AssemblyNameOfYourTestProject")] 

Once you do that, the error will go away (I just tried it myself).

Alternatively, you could have just used brute force reflection:

Assert.AreEqual(123, jsonResult.GetType().GetProperty("Id").GetValue(jsonResult, null)); 
like image 191
Kirk Woll Avatar answered Sep 28 '22 05:09

Kirk Woll


Having read the responses here and then looking further afield I found a 2009 msdn blog post with a different approach again. But.. in the comments was a very simple and very elegant solution by Kieran ... to use .ToString().

In your original case:

[HttpPost] public JsonResult Test() {     return Json(new {Id = 123}); } 

You could test by doing:

var jsonResult = controller.Test(); Assert.AreEqual("{Id = 123}", jsonResult.Data.ToString()); 

I much prefer this solution as it:

  • avoids changing the original code (InternalsVisibleTo, ExpandoObject),
  • avoids using MvcContrib and RhinoMocks (no issue with either of these but why add just to be able to test JsonResult?), and,
  • avoids using Reflection (adds complexity to the tests).
like image 20
Scotty.NET Avatar answered Sep 28 '22 04:09

Scotty.NET