开发者

Mock object fails to return required object

开发者 https://www.devze.com 2023-02-20 04:23 出处:网络
There is bound to be something obvious I\'m overlooking - there usually is when I have problems like this.

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,
         开发者_如何学JAVA   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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消