简体   繁体   中英

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

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.

In your SetupMethods, articleId is 0.

In your test, you set it to 1, so your .Setup is never being called. I would move your Setup into your test.

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

    Mock<IArticleRepository> repositoryMock = new Mock<IArticleRepository>();
    repositoryMock.Setup(x => x.GetById(articleId)).Returns(GetSampleArticle());

    controller = new HomeController(repositoryMock.Object);

    // 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 technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM