Question

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.

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top