I try to test my controller
[TestMethod]
public void Index()
{
AdminController controller = new AdminController();
ViewResult result = controller.Index() as ViewResult;
Assert.IsNotNull(result);
}
Here is Index() code:
public ActionResult Index()
{
var repository = new PostsRepository();
var posts = repository.GetAllPosts();
return View(posts);
}
Repository:
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();
}
}
But my Index() unit test fails with error:
Test method MvcBlog.Tests.Controllers.AdminControllerTest.Index threw exception:
System.NullReferenceException: Object reference not set to an instance of an object.
Stack Trace:
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
Why it is an error?
If i just run my application, it works correctly.
Source code download: http://dl.dropbox.com/u/14053604/MvcBlog.rar
The problem is the following line in your controller action:
Here you are tying your controller to a particular implementation of the repository making it very difficult to unit test in isolation. In order to achieve weaker coupling between your controller and data access layer I would recommend you using constructor injection:
Now your controller is completely decoupled from a particular implementation of a repository which might depend on a database, etc. Now you could use a mocking framework such as Rhino Mocks or Moq to provide a dummy implementation of this repository for the unit test.
Personally I like very much MVCContrib TestHelper which works with Rhino Mocks and allows for very elegant unit tests of controller actions. So once you’ve decoupled your controller from a specific implementation of the repository as I showed you could have the following unit test: