在单元测试中使用模拟时出现错误消息
Error message while working with mocking in unit test
抱歉标题不好。
我正在尝试进行一些单元测试。我有以下控制器:
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,然后添加正确的:
Select 解决方案资源管理器中的测试项目 -> 参考 -> 右键单击 System.Web.Mvc -> 删除 (How to: Add or Remove References)
然后添加 System.Web.Mvc 的正确版本,即 5.2.3.0
编辑
ASP.NET MVC 有一个 nuget package。这是添加程序集的正确方法
抱歉标题不好。
我正在尝试进行一些单元测试。我有以下控制器:
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,然后添加正确的:
Select 解决方案资源管理器中的测试项目 -> 参考 -> 右键单击 System.Web.Mvc -> 删除 (How to: Add or Remove References)
然后添加 System.Web.Mvc 的正确版本,即 5.2.3.0
编辑
ASP.NET MVC 有一个 nuget package。这是添加程序集的正确方法