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 に答える