我是新來的ASP.Net MVC - 我讀的書MVC和下面的代碼測試分頁的示例應用程序的書:ASP.Net MVC - Mocks做什麼?
[TestMethod]
public void Can_Paginate()
{
// Arrange
// - create the mock repository
Mock<IProductRepository> mock = new Mock<IProductRepository>();
mock.Setup(m => m.Products).Returns(new Product[] {
new Product {ProductID = 1, Name = "P1"},
new Product {ProductID = 2, Name = "P2"},
new Product {ProductID = 3, Name = "P3"},
new Product {ProductID = 4, Name = "P4"},
new Product {ProductID = 5, Name = "P5"}
}.AsQueryable());
// create a controller and make the page size 3 items
ProductController controller = new ProductController(mock.Object);
controller.PageSize = 3;
// Action
IEnumerable<Product> result = (IEnumerable<Product>)controller.List(2).Model;
// Assert
Product[] prodArray = result.ToArray();
Assert.IsTrue(prodArray.Length == 2);
Assert.AreEqual(prodArray[0].Name, "P4");
Assert.AreEqual(prodArray[1].Name, "P5");
}
的問題是,什麼Mock
辦?我無法直接將Product
陣列傳遞給ProductController
嗎?爲什麼這一輪?
您可能還想讀kzu的「嘲笑,存根和假貨:它是一個連續體」http://blogs.clariusconsulting.net/kzu/mocks-stubs-and-fakes-its-a-continuum/ – gerryLowry
@gerryLowry很好的資源鏈接! – stack72