2011-01-21 85 views
1

我正努力理解測試的第一部分正在發生什麼。似乎無法弄清這個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 [] {product} .AsQueryable());

它實際上告訴存儲庫中的產品返回一個可查詢的新產品?但爲什麼?

如果任何有單元測試經驗的人都可以幫助我,我會很高興!

謝謝。

+0

您可能想要考慮查看更現代的隔離(模擬)框架,如Moq。請參閱http://polldaddy.com/poll/3746444/ – TrueWill 2011-01-21 23:08:32

回答

0

找到解決方案。

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

它實際上設置了存儲庫,爲每個產品返回一個可查詢的新產品。

相關問題