How do you unit test perform ASP.Net MVC JsonResult actions?

I still understand a few points around a module testing an ASP.Net MVC2 application using NUnit.

In general, testing my ActionResults, models, repositories, etc. straightforwardly, but I didn’t have to test Ajax methods before, and I would like to get some recommendations on how best to do this.

Thanks in advance.

+5
source share
2 answers

Testing the action of the controller returning JsonResult should not be different from testing other actions. Consider the following scenario:

public class MyModel
{
    public string Name { get; set; }
}

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return Json(new MyModel { Name = "Hello World" });
    }
}

unit test (, MSTest, NUnit atm, ):

// arrange
var sut = new HomeController();

// act
var actual = sut.Index();

// assert
Assert.IsInstanceOfType(actual, typeof(JsonResult));
var jsonResult = (JsonResult)actual;
Assert.IsInstanceOfType(jsonResult.Data, typeof(MyModel));
var model = (MyModel)jsonResult.Data;
Assert.AreEqual("Hello World", model.Name);
+6

IMO, Ajax ( , , ), . , Selenium RC WatiN.

0

All Articles