Код: Выделить всё
using SportsStore.domain.Abstract;
using SportsStore.domain.Entities;
namespace SportsStore.WebUI.Controllers
{
public class ProductController : Controller
{
private IProductRepository repository;
public int PageSize = 4;
//Declar the dependency on IProductRepository
public ProductController(IProductRepository productRepository)
{
this.repository = productRepository;
}
// GET: Product
public ViewResult List(int page = 1)
{
return View(repository.Products.OrderBy(p => p.ProductID).Skip((page-1) * PageSize).Take(PageSize));
}
}
}
Вот мой модульный тест:
Код: Выделить всё
using SportsStore.domain.Abstract;
using SportsStore.domain.Entities;
using SportsStore.WebUI.Controllers;
using System.Collections.Generic;
using System.Web.Mvc;
namespace SportsStore.UnitTests
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
}
[TestMethod]
public void Can_Paginate()
{
//Arrange
Mock mock = new Mock();
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" }
});
ProductController controller = new ProductController(mock.Object);
controller.PageSize = 3;
//Act
IEnumerable
result = (IEnumerable)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");
}
}
}
[img]https:// i.sstatic.net/b8S5K.png[/img]
Что это значит?
Подробнее здесь: https://stackoverflow.com/questions/332 ... -unit-test
Мобильная версия