2011-01-21 5 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

+0

を参照してください。すでにmoqを使用しています。 – Rushino

答えて

0

検出された溶液。

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

実際には、クエリ可能な新しい製品を各製品ごとに返すようにリポジトリを設定しました。