Pro ASP.NET MVC 3 Frameworkという本を読みました。単体テストに少し問題があります。このテストを実行すると、結果変数が null であるため合格しませんでした。
// 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 プロジェクトではすべてがうまく機能することです。問題なく商品一覧表示。