我读了一本书 Pro ASP.NET MVC 3 Framework。我对单元测试有一点问题。当我运行这个测试时,它没有通过,因为结果变量为空。
// 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());
// Arrange - create a controller and make the page size 3 items
ProductController controller = new ProductController(mock.Object);
controller.PageSize = 3;
// Action
ProductsListViewModel result = (ProductsListViewModel)controller.List(2).Model;
// Assert
PagingInfo pageInfo = result.PagingInfo;
产品控制器:
public ViewResult List(int page = 1)
{
ProductsListViewModel viewModel = new ProductsListViewModel
{
Products = repository.Products
.OrderBy(p => p.ProductID)
.Skip((page - 1) * PageSize)
.Take(PageSize),
PagingInfo = new PagingInfo
{
CurrentPage = page,
ItemsPerPage = PageSize,
TotalItems = repository.Products.Count()
}
};
return View(viewModel);
}
我是 mvc 的新手,但对我来说奇怪的是,在 WebUI 项目中一切都很好。产品列表显示没有任何问题。