似乎无法弄清楚这个nunit测试的第一部分

时间:2011-01-21 22:50:08

标签: nunit nunit-mocks

我很难理解测试的第一部分发生了什么。

[Test]
public void Can_Delete_Product()
{
      // Arrange: Given a repository containing some product...
      **var mockRepository = new Mock<IProductsRepository>();
      var product = new Product { ProductID = 24, Name = "P24" };
      mockRepository.Setup(x => x.Products).Returns(new[] { product }.AsQueryable());**

      // Act: ... when the user tries to delete that product
      var controller = new AdminController(mockRepository.Object);
      var result = controller.Delete(24);

      // Assert: ... then it's deleted, and the user sees a confirmation
      mockRepository.Verify(x => x.DeleteProduct(product));
      result.ShouldBeRedirectionTo(new { action = "Index" });
      controller.TempData["message"].ShouldEqual("P24 was deleted");
}

为什么这样? mockRepository.Setup(x =&gt; x.Products).Returns(new [] {product} .AsQueryable());

它实际上告诉存储库中的产品返回一个可疑的新产品?但为什么呢?

如果任何有单位测试经验的人可以帮助我,我会很高兴!

感谢。

1 个答案:

答案 0 :(得分:0)

找到解决方案。

mockRepository.Setup(x =&gt; x.Products).Returns(new [] {product} .AsQueryable());

它实际上设置了存储库,以便为每个产品返回一个可查询的新产品。