似乎无法弄清楚这个 nunit 测试的第一部分
我很难理解测试第一部分发生了什么。
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到解决方案。
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.