【发布时间】:2015-10-19 21:38:51
【问题描述】:
抱歉标题不好。
我正在尝试做一些单元测试。我有以下控制器:
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));
}
}
我要对页面分页进行单元测试。
这是我的单元测试:
[TestMethod]
public void Can_Paginate()
{
//Arrange
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" }
});
ProductController controller = new ProductController(mock.Object);
controller.PageSize = 3;
//Act
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");
}
我在测试文件中收到以下错误消息:
这是什么意思?
【问题讨论】:
-
您的项目 dll 使用比您在测试项目中引用的 dll 更新的 dll。只需从您的测试项目中删除 dll,然后添加正确的。
-
@OldFox:如何删除 dll?
-
我将其发布为答案...
标签: c# unit-testing mocking