Search code examples
asp.net-mvc-3unit-testingtddrhino-mocks

Rhino Mocks Func asserting Test


I need to test that a particular Func is called in Method List ex:

public class ProductController : BaseController
    {
        private readonly Func<IProductRepository> prodRepo;
        public ProductController(Func<IProductRepository> _prodRepo)
       {
            prodRepo = _prodRepo;
       } 
        public ActionResult List(string applicationID)
        {
            var products = prodRepo().GetForApp(applicationID).ToList();
            return PartialView("_List",products);
        }
}

in this case i need to verify that prodRepo().GetForApp(applicationID) whase called.


Solution

  • Do you really need to verify that the Func is called? Or do you need to verify that the ProductController retrieves the products correctly?

    If it's the latter, just set up a mock of the IProductRepository to return some products, pass it in via a lambda, and assert that the products you get are the right ones. Generally, if something is providing information (the "arrange" phase in act / arrange / assert, or the "given" in given / when / then) then you want a stub rather than a mock.

    The only time you really need to use a mock and verify that something has been called is when the class under test is delegating a responsibility - for instance, saving a product in the repository.

    Also, please check out Moq... it's a bit easier to set up stubs in Moq IMO.