Brendan Vogt
Brendan Vogt

Reputation: 26028

Pointers need on first test with NUnit and MVC Contrib

I'm using ASP.NET MVC 2, NUnit, Moq and MVC Contrib. I have written my first unit test ever, and I have a couple of questions regarding this test. My scenario is that I have an Index view. On this view I have a grid that displays all the news items.

Here is my INewsRepository class:

public interface INewsRepository
{
   IEnumerable<News> FindAll();
}

My test class with the test method:

public class NewsControllerTest :TestControllerBuilder
{
   private Mock<INewsRepository> mockNewsRepository;
   private NewsController newsController;

   [SetUp]
   public void Init()
   {
      mockNewsRepository = new Mock<INewsRepository>();
      newsController = new NewsController(mockNewsRepository.Object);
      InitializeController(newsController);
   }

   [Test]
   public void NewsController_Index()
   {
      // Arrange
      var news = new Mock<IEnumerable<News>>();
      mockNewsRepository.Setup(r => r.FindAll()).Returns(news.Object).Verifiable();

      // Act
      ActionResult actual = newsController.Index();

      // Assert
      mockNewsRepository.Verify();
      actual
         .AssertViewRendered()
         .ForView("Index")
         .WithViewData<News[]>()
         .ShouldBe(news);
   }
}

My view:

public ActionResult Index()
{
   FakeNewsRepository fakeNewsRepository = new FakeNewsRepository();
   IEnumerable<News> news = fakeNewsRepository.FindAll();
   return View(news);
}

I need some pointers on the way that I did it. Am I in the correct direction? What should I add, what should I leave out? I want to do unit testing, am I mixing it with integration testing? Any extra advice would be appreciated.

When I run this test in the NUnit GUI console then I get an error back and I'mnot sure what it means:

MyProject.Web.Tests.Controllers.NewsControllerTest.NewsController_Index:
Moq.MockVerificationException : The following setups were not matched:
INewsRepository r => r.FindAll()

Upvotes: 1

Views: 331

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038810

public ActionResult Index()
{
    FakeNewsRepository fakeNewsRepository = new FakeNewsRepository();
    IEnumerable<News> news = fakeNewsRepository.FindAll();
    return View(news);
}

You cannot mock the repository that is hardcoded like this in your action. You are instantiating it inside the action, you will never be able to unit test this and mock the repository. The repository needs to be injected as a dependency. You could use an interface and pass this interface to the constructor of your controller:

public class HomeController: Controller
{
    private readonly IRepository _repository;
    public class HomeController(IRepository repository)
    {
        _repository = repository;
    }

    public ActionResult Index()
    {
        IEnumerable<News> news = _repository.FindAll();
        return View(news);
    }
}

Now in your unit test you could pass the mocked instance of your repository to the controller constructor and define expectations.

Also notice that MVCContrib.TestHelper is designed to work with Rhino Mocks. I am not quite sure whether it works fine with Moq.

Upvotes: 3

Related Questions