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

发布于 2024-10-13 04:16:23 字数 1001 浏览 1 评论 0原文

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

[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[] { 产品 }.AsQueryable());

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

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

谢谢。

Im struggling to understand what going on in the first part of the test.

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

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

It actually tell the products in the repository to return a new product which is asqueryable ? but why?

If anyone with some experience in unit tests could help me i would be glad!

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟凡古楼 2024-10-20 04:16:23

找到解决方案。

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

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

Found solution.

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

It actually setup the repository to return for each product a new product which is queryable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文