在单元测试中使用模拟时出现错误消息

时间:2015-10-19 11:33:04

标签: c# unit-testing mocking

抱歉标题不好。

我试图做一些单元测试。我有以下控制器:

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");
}

我在测试文件中收到以下错误消息:

enter image description here

这是什么意思?

1 个答案:

答案 0 :(得分:1)

您的项目dll使用比您在测试项目中引用的dll更新的dll。只需从测试项目中删除dll,然后添加正确的dll:

在Solution explorer中选择测试项目 - &gt;参考文献 - &gt;右键单击System.Web.Mvc - &gt;删除(How to: Add or Remove References

添加正确版本的System.Web.Mvc,即5.2.3.0

修改

ASP.NET MVC有一个nuget package。这是添加程序集的正确方法