1

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

[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 => x.Products).Returns(new[] { product }.AsQueryable());

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

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

谢谢。

4

1 回答 1

0

找到解决方案。

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

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

于 2011-02-01T14:24:15.807 回答