帮助使用Moq

时间:2009-12-18 01:21:45

标签: moq

我有一个接收数据库工厂的类。

public class ArticleManager
{
     private IDAOFactor _factory;

     public ArticleManager(IDAOFactory factory)
      {
            this._factory = factory;
       }

}

使用moq,如何创建ArticleManager的实例?

我试过了:

var mockFactory = new Mock<IDAOFactory>();
ArticleManager am = new ArticleManager(mockFactory);

但这不是编译。

1 个答案:

答案 0 :(得分:1)

mockFactory是moq的代理类。要在模拟中公开ArticleManager对象实例,您将使用mockFactory.Object,如下所示:

var mockFactory = new Mock<IDAOFactory>();
ArticleManager am = new ArticleManager(mockFactory.Object);