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()

Answers


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.


Need Your Help

Using Ajax to send form data

php ajax webforms

I have a HTML/PHP form using using post to send form data to a script php file. I want to send the data with out the page reloading upon clicking the submit button.

Dynamically creating Expression Blend controls in WPF C#

c# wpf wpf-controls expression-blend

I have created a button in Expression Blend 4. I want to dynamically create instances of this button at run time.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.