While you can return any data type from a webapi action method, JsonResult is one of the most convenient return type because of flexibility it provides. You just need to pass an object to Json function and it’ll generate a valid Json response for your data.

When writing unit tests in such cases, you may feel the need to deserialize the Json response to extract it’s values.

In this article, I’ll just provide you steps to write unit tests for JsonResult. I’ll not get into the details regarding creating webapi and test projects. You can read the already written articles here and here. I’d rather jump straight to the problem and the solution.

The Controller & Action Methods

Suppose this is the action method that returns some products. A simple product class looks like this. For the sake of brevity, I’ve created just one property called Name.

And this is the how the controller and action methods look

Needless to say, in a real world application, the product data will be fetched from some data provider source such as a database, a web service etc.

Now to test this action, let’s write a test. As I said, I won’t go through the process of setting up the test project. You can go through the above links to do that.

Inside your test file, create following two methods.

The method actually uses reflection to read the content of your json result for a given property and returns the property value if the property is found. If it’s not found, it throws an exception.

We have just create another method that takes IHttpActionResult  as the parameter. The reason is simple and easy way to handle all kinds of responses (Xml, Json or something else may be?). You just need to modify the second method to call appropriate methods for other response types. We for the sake of brevity stick to the JsonResult type.

Now let’s setup a test

Note that I’m using NUnit as my unit test framework. However the process is same for any framework for that matter.

If you run the test, you’d get a success response from the test results. This way you should be able to test the JsonResult from webapi.

But there is a problem

The default way of parsing JsonResult will not be able to read the Json keys which are below level one. To read the keys beyond level one (i.e. nested json), the following assertions won’t work.

In the second assertion above, we have queried the Products property of JsonResult as a dynamic object. So inside json, the result looks like

Because by default, the nested keys of dynamic object are internal, we won’t be able to access the Count property of the Products. The result, the test fails even if the function worked correctly.


The solution involves telling our webapi to expose these internal properties to our test projects. This is achieved using InternalsVisibleToAttribute . What this attribute does is that it specifies that some other project is a friend to current project and hence should be given access to the internal properties.

So to make the above test work, open your Global.asax file of your webapi project and add the following above the namespace specification.

This way, you are specifying that your test project is a friend to your webapi project and so it’s internal properties should be accessible via test project.

Now if you run the tests again, they will succeed.

Happy TDD