模拟对象无法返回所需的对象

时间:2011-03-24 23:32:26

标签: c# asp.net-mvc-3 nunit moq ninject-2

肯定会有一些明显的东西我会忽略 - 通常情况下我会遇到这样的问题。

我有一个控制器,只根据提供的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);
    }

}

我正在使用Moq嘲笑这个:

[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);
    }
}

上面讨论的“GetSampleArticle”方法如下所示:

    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;
    }

但是我仍然在为模型获取null类型。那么我忘记了什么?

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

2 个答案:

答案 0 :(得分:5)

另一个想法是使用It.IsAny()方法来避免必须查找硬编码值:

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

这样做是安全的,因为你不关心articleId的实际价值,而不是它的检索机制。

答案 1 :(得分:1)

在SetupMethods中,articleId为0。

在测试中,您将其设置为1,因此永远不会调用.Setup。我会将您的安装程序移动到您的测试中。

[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);
}