Unit testing in ASP.NET MVC Framework

    While making a report on the ASP.NET MVC Framework, I noticed that this framework gives the developer the opportunity to use TDD practices in development and immediately got a question about what TDD is, why and how exactly the ASP.NET MVC Framework gives these very capabilities.

    ASP.NET MVC Framework + TDD

    The developer receives the very first hint of TDD as soon as he tries to create a project using the ASP.NET MVC Web application template. It will be immediately proposed to create another project, for tests. In this case, it is proposed to use standard MS testing technologies, but there are solutions on the Internet how to add xUnit, MbUnit, and nUnit . If you use this hint, we will get another project in which all the necessary libraries for testing will be connected, a cs-file with one class and method will be created, then you will have to do everything with pens.

    Starting with Preview 2, all Action methods should return a result. The result can be any class inherited from the base type ActionResult (ViewResult, JsonResult, ActionRedirectResult and so on). Thus, when testing, you can easily check the data that your method passes to View (ViewData).
    ViewResult data = blogController.Comments(5);
    Assert.AreEqual(expect, data.ViewData.Model);
    * This source code was highlighted with Source Code Highlighter.

    In addition, if the method returns a ViewResult, you can just as easily check which View will be used.
    ViewResult data = blogController.Comments(5);
    Assert.AreEqual("comments", data.ViewName);

    * This source code was highlighted with Source Code Highlighter.

    Also in the framework itself there is not a single sealed class, and all types are built on interfaces (IView, IController) or have their own base abstract class (HttpResponseBase, HttpSessionStateBase). This approach will easily allow a class to get wet during testing. Which, for example, allows you to test the routes.

      var httpContextMock = new Mock();
      var requestMock = new Mock();
      httpContextMock.Expect(c => c.Request).Returns(requestMock.Object);
      requestMock.Expect(r => r.AppRelativeCurrentExecutionFilePath)

      RouteData routeData = routes.GetRouteData(httpContextMock.Object);
      Assert.IsNotNull(routeData, "Should have found the route");

    * This source code was highlighted with Source Code Highlighter.

    So MS, having chosen the TDD approach for this technology, are forced to use those solutions that will allow end users to easily follow their example.

    So in RC1 it became possible to automatically create a View for the desired Action Method, why not go ahead and build in the ability to automatically create a test set (Of course, it’s not possible to cover all test cases, but the most repeated ones ... Why not.)

    Well, in the end there’s one test for my method and how it looks in nUnit-e.

    A small example of a test and method.

    1.   [TestFixture]
    2.   public class TestCase4BlogsController
    3.   {
    5.     [Test]
    6.     public void TestGetCommentsList_IfPutIDExistBlog()
    7.     {
    8.       MockModel mockModel = new MockModel();
    9.       BlogController blogController = new BlogController();
    10.       ArrayList expect = new ArrayList {new { Data = "Это вам не рыбу, динамитом глушить", Date = "12.02.2009", Author = "Иван Петрович"},
    11.                         new { Data = "Дык динамитом, оно эффективнее", Date = "13.02.2009", Author = "Пацак"},
    12.                         new { Data = "И что ты потом будешь делать с этой рыбой ???", Date = "13.02.2009", Author = "Иван Петрович"} };
    15.       mockModel.comments = expect;
    16.       blogController.Model = mockModel as IBlogModel;
    19.       ViewResult data = blogController.Comments(5);
    20.       Assert.AreEqual(expect, data.ViewData.Model);
    21.     }
    23.   }
    * This source code was highlighted with Source Code Highlighter.

    1.   public class MockModel : IBlogModel
    2.   {
    3.     public ArrayList comments = null;
    5.     #region IBlogModel Members
    7.     public ArrayList GetComment4Blog(int blogId)
    8.     {
    9.       return comments;
    10.     }
    12.     #endregion
    13.   }
    * This source code was highlighted with Source Code Highlighter.

    Before the method was written.

    Method implementation.
    1.     [AcceptVerbs(HttpVerbs.Get)]
    2.     public ViewResult Comments(int blogId)
    3.     {
    4.       return View("comments", Model.GetComment4Blog(blogId));
    5.     }
    * This source code was highlighted with Source Code Highlighter.

    After compilation.

    Also popular now: