ASP.NET MVC控制器单元测试失败

时间:2011-01-29 21:12:41

标签: c# asp.net asp.net-mvc unit-testing asp.net-mvc-2

我尝试测试我的控制器

[TestMethod]
    public void Index()
    {
        AdminController controller = new AdminController();
        ViewResult result = controller.Index() as ViewResult;
        Assert.IsNotNull(result);
    }

以下是Index()代码:

public ActionResult Index()
        {
            var repository = new PostsRepository();
            var posts = repository.GetAllPosts();

            return View(posts);
        }

存储库:

public class PostsRepository : IPostsRepository
    {
        PostsDataContext _dataContext = new PostsDataContext();

        public IQueryable<Post> GetAllPosts()
        {
            var posts = from t in _dataContext.Posts select t;
            return posts;
        }

        public Post GetPostById(int id)
        {
            var post = from t in _dataContext.Posts
                       where t.id == id
                       select t;
            return post.First();
        }
    }

但我的Index()单元测试失败并出现错误:

Test method MvcBlog.Tests.Controllers.AdminControllerTest.Index threw exception: 
System.NullReferenceException: Object reference not set to an instance of an object.

堆栈追踪:

MvcBlog.Models.PostsDataContext..ctor() in C:\Users\cL1Nk3r\Documents\Visual Studio 2010\Projects\MvcBlog\MvcBlog\Models\Posts.designer.cs: line 38
MvcBlog.Repository.PostsRepository..ctor() in C:\Users\cL1Nk3r\Documents\Visual Studio 2010\Projects\MvcBlog\MvcBlog\Repository\PostsRepository.cs: line 11
MvcBlog.Controllers.AdminController.Index() in C:\Users\cL1Nk3r\Documents\Visual Studio 2010\Projects\MvcBlog\MvcBlog\Controllers\AdminController.cs: line 19
MvcBlog.Tests.Controllers.AdminControllerTest.Index() in C:\Users\cL1Nk3r\Documents\Visual Studio 2010\Projects\MvcBlog\MvcBlog.Tests\Controllers\AdminControllerTest.cs: line 19

为什么会出错?
如果我只是运行我的应用程序,它可以正常工作。

源代码下载:http://dl.dropbox.com/u/14053604/MvcBlog.rar

1 个答案:

答案 0 :(得分:6)

问题是控制器操作中的以下行:

var repository = new PostsRepository();

在这里,您将控制器绑定到存储库的特定实现,这使得单独进行单元测试变得非常困难。为了实现控制器和数据访问层之间的耦合较弱,我建议您使用构造函数注入:

public class PostsController: Controller
{
    private readonly IPostsRepository _repository;
    public PostsController(IPostsRepository repository)
    {
        _repository = repository;
    }

    public ActionResult Index()
    {
        var posts = _repository.GetAllPosts();
        return View(posts);
    }
}

现在,您的控制器与可能依赖于数据库等的存储库的特定实现完全分离。现在您可以使用模拟框架(如Rhino MocksMoq)来提供虚拟实现用于单元测试的存储库。

就我个人而言,我非常喜欢使用Rhino Mocks的MVCContrib TestHelper,并且可以对控制器动作进行非常优雅的单元测试。因此,一旦您将控制器与存储库的特定实现分离,如我所示,您可以进行以下单元测试:

[TestMethod]
public void PostsController_Index_Action_Should_Fetch_All_Posts_From_Repository()
{
    // arrange
    var postsRepositoryStub = MockRepository.GenerateStub<IPostsRepository>();
    var sut = new PostsController(postsRepositoryStub);
    var expectedPosts = new Post[0];
    postsRepositoryStub.Stub(x => x.GetAllPosts).Return(expectedPosts);

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

    // assert
    actual
        .AssertViewRendered()
        .WithViewData<IEnumerable<Post>>()
        .ShouldBe(expectedPosts);
}