How to unit test the JsonResult?

Last post 02-11-2009 2:06 AM by NingDev@Gmail.com. 4 replies.

Sort Posts:

  • How to unit test the JsonResult?

    02-10-2009, 10:39 PM

    Dear friends,

    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.AreEqual("{ isSuccess = True, message = ="It's success!"  }", result.Data.ToString(), true);

    Now I wanna find one best practices to test the JosnResult, it's a json string. It's possible very long, comparing the string is not a good idea. Any help? 

  • Re: How to unit test the JsonResult?

    02-10-2009, 11:48 PM
    • Contributor
      4,792 point Contributor
    • KaziManzurRashid
    • Member since 03-09-2003, 3:04 PM
    • Dhaka, Bangladesh
    • Posts 882

    Try this:

    In Controller:

    public ActionResult JsonMethod(string id)
    {
    return Json(new CustomViewData { isSuccess = true});
    }

     
    And In Test:
     

    public void TestJsonMethod()
    {
    var controller = new JsonController();

    var viewModel = ((JsonResult) controller.JsonMethod("foo")).Data as CustomViewData;

    Assert.IsTrue(viewModel.isSuccess);
    }
     

     

    Long Live .NET
    Kazi Manzur Rashid (Amit)
    _________________________
    Web: http //dotnetshoutout.com
    Blog: http://weblogs.asp.net/rashid
    Twitter: http://twitter.com/manzurrashid
  • Re: How to unit test the JsonResult?

    02-11-2009, 12:23 AM

    Thanks for your reply! Your means defining a new type to involve these information, it's a good idea, but as we know, there are many sorts of json result according the special requirements, I want to use one anonymous object. It's possible my idea is not correct. Could you please give me your idea to use the JosnResult.

     Thanks and Best Regards! 

  • Re: How to unit test the JsonResult?

    02-11-2009, 1:37 AM
    Answer
    • Contributor
      4,300 point Contributor
    • levib
    • Member since 07-23-2007, 11:50 PM
    • Redmond, WA
    • Posts 763
    • AspNetTeam

    You can read the object passed to Json() via the JsonResult.Data property.  If you pass an anonymous type into the Json() method, then you can't access the properties on JsonResult.Data directly since you can't reference its type, but you can use a wrapper to get at them.  For example, try passing the Data object to the System.Web.Routing.RouteValueDictionary() constructor, and you will get back a <string, object> dictionary where the keys are the property names and the values are the property values of the anonymous object.  Then you can perform assertions against this dictionary directly.

  • Re: How to unit test the JsonResult?

    02-11-2009, 2:06 AM


    Thanks for levib help!

    It's a good solutioin!

    Please reference the following codes

    var result = controller.JsonMethod() as JsonResult; 

    IDictionary<string, object> wrapper = (IDictionary<string, object>)new System.Web.Routing.RouteValueDictionary(result.Data);

    Assert.AreEqual(true, wrapper["isExisted"], "");
    Assert.AreEqual("", wrapper["message"], "");

     
     
Page 1 of 1 (5 items)