28/03/2016 - 1 minute read

How To Test JsonResult in ASP.NET MVC

Written by

Gary Lewis Cheetham

Automotive marketing specialist from Manchester, UK

Here's a quick NuGet of wisdom! (Haha, just kidding, there is no wisdom in NuGet)

Writing a unit test for an ActionResult Json() is a bit of a head-scratcher for some, but all it requires is a simple cast operation.

Example controller:

    public ActionResult Index()
    {
        return Json(productsData, JsonRequestBehavior.AllowGet);
    }

Example test: (NUnit 3.x)

    [Test]
    public void GetActionReturnsProductsAsJson()
    {
        var mockProductsData = new List<IProduct> { /* ... */ };
        productsController.setData(mockProductsData);
        JsonResult result = productsController.Index() as JsonResult;
        Assert.That(
            result.Data as List<IProduct>,
            Is.EqualTo(mockProductsData));
    }

There are two casts going on here:

  • The first cast (productsController.Index() as JsonResult) makes sure that we're dealing with a JsonResult in the test and not an ActionResult.
  • The second cast result.Data as List<IProduct> takes the Data property of the JsonResult and casts it to a List of IProduct that we can compare against our mock data.

Note that you could have achieved the same thing using the alternative cast syntax:

JsonResult result = (JsonResult)productsController.Index();

But the as keyword is preferred, because it will return null instead of an exception if the cast is not possible.

About the author

Gary Lewis Cheetham is an automotive marketing specialist, born and raised in Oldham, Greater Manchester. He spent his teenage years working at the family dealership after school, learning the ropes of the car trade from the inside out.

After moving on to working in marketing, Gary Lewis founded GL Digital in 2020 when he noticed a need for direct-to-consumer marketing platforms within the auto industry. He now strives every day to help independent dealers in the UK and US realise their potential. Gary also loves Formula 1 and motorsport.

More Articles

SEO For Car Dealerships: Everything You Need to Know

SEO For Car Dealerships: Everything You Need to Know

We will be the first to say it: SEO for car dealerships is officially over in 2022. Car dealers fail at SEO time and time again, because…

5 minute read - 07/02/2022

Everything you need to know about Conversion Rate Optimisation for Car dealerships in 2022

Everything you need to know about Conversion Rate Optimisation for Car dealerships in 2022

This article will cover how dealerships can do conversion rate optimisation on their website and sales funnels, including for lead…

6 minute read - 27/01/2022

How to run successful Email Marketing for Car Dealerships in 2022

How to run successful Email Marketing for Car Dealerships in 2022

Email marketing is one of the most effective digital marketing strategies for car dealerships to build relationships with customers. It…

5 minute read - 27/01/2022

See More