How do I unit test controllers for an asp.net mvc site that uses StructureMap and NHibernate?

Posted by Jim Geurts on Stack Overflow See other posts from Stack Overflow or by Jim Geurts
Published on 2010-03-22T16:11:27Z Indexed on 2010/03/22 16:31 UTC
Read the original article Hit count: 332

I have an asp.net mvc2 application that is using StructureMap 2.6 and NHibernate 3.x. I would like to add unit tests to the application but am sort of at a loss for how to accomplish it.

Say I have a basic controller called Posts that has an action called Index. The controller looks something like:

public class PostsController : Controller {

  private readonly IPostService _postService;
  public PostsController(IPostService postService) {
    _postService = postService;
  }

  public ActionResult Index() {
    return View(_postService.QueryOver<Post>().Future());
  }
}

If I wanted to create an nunit test that would verify that the index action is returning all of the posts, how do I go about that? If mocking is recommended, do you just assume that interaction with the database will work?

Sorry for asking such a broad question, but my web searches haven't turned up anything decent for how to unit test asp.net mvc actions that use StructureMap (or any other IOC) and NHibernate.

btw, if you don't like that I return a QueryOver object from my post service, pretend it is an IQueryable object. I'm using it essentially in the same way.

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about unit-testing