Disadvantages of the ASP.NET MVC Framework - Category Method
(Page 3 of 4 )
Next, let’s take a look at the unit test related to the Category method, as follows:
//…… (omitted)
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MvcApplication.Controllers;
using MvcApplication.Models;
using System.Web.Mvc;
using System.Web.Routing;
using Moq;
namespace MvcApplicationTest
{
[TestClass()]
public class ProductsControllerTest
{
[TestMethod]
public void Category()
{
var categories = new List<Category>();
categories.Add(new Category { CategoryID=123,CategoryName = "Roomba"});
var repository = new Mock<NorthwindRepository>();
repository.Expect(r => r.Categories).Returns(categories.AsQueryable());
var controller = new ProductsController(repository.Object);
var result = controller.Category(123) as ViewResult;
Assert.IsNotNull(result, "Expected the result to be a render view result");
Assert.AreEqual("Roomba", result.ViewData["CategoryName"]);
var viewData = result.ViewData.Model as Category ;
Assert.AreEqual(123, viewData.CategoryID);
Assert.AreEqual("Roomba", viewData.CategoryName);
}
First, you should have noticed here that we have not only referred to the built-in namespace Microsoft.VisualStudio.TestTools.UnitTesting, but also to Moq. The current version of Moq is 2.5.3. Although this framework is simple, due to introducing the new lambda expression, Moq can provide a clear grammar form to describe the expectation, parameter constrains, and returned values. For detailed information, you can refer to this blog .
In the unit test programming, when complex objects are used (in this case the NorthwindRepository typed variable) or difficult to implement for the time being, we can rest on the Mock object to simulate the target object, which is properly consistent with the basic idea of TDD. Here, we will use the Moq class provided by the Moq framework to mock the NorthwindRepository class, as follows:
var repository = new Mock<NorthwindRepository>();
Next, we use the following sentence to set up the expectation of the set of the Category objects:
repository.Expect(r => r.Categories).Returns(categories.AsQueryable());
Then, we create an instance of the ProductsController class and pass in the repository.Object. Note here that the repository represents the mock object, while the repository.Object refers to the target object that the mock object mocks.
Since the last several Asserts are easy to follow, we won't dwell on them.
Well, how are you feeling now? It’s not as easy as you imagine if you do not possess the abundant and solid basic TDD knowledge and all the other required programming skills, isn’t it? In fact, there are many more parts that need to be tested: the Route, the ViewData, the HTTPContext-related items, etc.
Another fact is that for now the MVC framework has not even published its normal release version, and therefore, no detailed help documents can be found (so most of the reference material can only be hunted down from blogs).
Anyway, the ASP.NET MVC is a new and alternate architecture for your ASP.NET applications, with many new features completed and many more to be added.
Next: Conclusion >>
More ASP.NET Articles
More By Xianzhong Zhu