Search code examples
c#asp.net-mvc-3nunitmoqninject-2

Mock object fails to return required object


There is bound to be something obvious I'm overlooking - there usually is when I have problems like this.

I have a controller that simply returns a news article based on a supplied ID:

[HandleError]
public class HomeController : Controller
{
    private readonly IArticleRepository articleRepository;

    public HomeController(IArticleRepository Repository)
    {
        articleRepository = Repository;
    }

    public ActionResult Index()
    {
        return View("Index");
    }

    // Here's the bit we're interested in
    public ActionResult Article(int id)
    {
        var article = articleRepository.GetById(id);

        return View("Article", article);
    }

}

I'm mocking this using Moq like so:

[TestFixture]
public class HomeControllerTests
{
    HomeController controller;
    int articleId;
    Article model;

    [TestFixtureSetUp]
    public void SetupMethods()
    {
        Mock<IArticleRepository> repositoryMock = new Mock<IArticleRepository>();
        repositoryMock.Setup(x => x.GetById(articleId)).Returns(GetSampleArticle());

        controller = new HomeController(repositoryMock.Object);
    }

    [Test]
    public void Article_Action_Returns_Requested_Article()
    {
        // Arrange
        model = new Article();
        articleId = 1;

        // Act
        ActionResult result = controller.Article(articleId);

        // Assert
        var viewResult = ((ViewResult)result);
        var returnedModel = viewResult.Model;
        Assert.IsInstanceOf<Article>(viewResult.Model);
        //Assert.AreEqual(articleId, returnedModel.ID);
    }
}

The "GetSampleArticle" method in question above just looks like this:

    private Article GetSampleArticle()
    {
        Article article = new Article()
        {
            Archived = false,
            Body = "<p>This is a dummy sample article for use in our mocks.</p>",
            EndDate = DateTime.Today.AddDays(30),
            ID = 1,
            Priority = 3,
            StartDate = DateTime.Today,
            Title = "Sample Article"
        };

        return article;
    }

However I'm still getting a null type for the model. So what have I forgotten?

News.Tests.Controllers.HomeControllerTests.Article_Action_Returns_Requested_Article:
  Expected: instance of <News.Data.Article>
  But was:  null

Solution

  • Another idea is to use the It.IsAny() method to avoid having to look for a hardcoded value at all:

    repositoryMock.Setup(x => x.GetById(It.IsAny<int>())).Returns(GetSampleArticle());
    

    Which is safe to do since you're not concerned with the actual value of the articleId so much as the mechanics of its retrieval.