在复杂的类引用上使用moq

时间:2016-01-06 21:07:46

标签: c# unit-testing oop moq

我在[TestInitialize]

中有这个
var classHelper = new classHelper(mockSessionRepository.Object, new SecurityTokenAgent(), AuthorityReporting.Instance, new UserClientProcessor());

locationRepo = new LocationRepo(mockXmlReader.Object, classHelper);

我想到了classHelper我会做的事情:

mockClassHelper = new Mock<IClassHelper>(mockSessionRepository.Object, new SecurityTokenAgent(), AuthorityReporting.Instance, new UserClientProcessor());

locationRepo = new LocationRepo(mockXmlReader.Object, mockSecurityHelper.Object);

这似乎是合乎逻辑的,它似乎也打破了我的所有[TestMethods]。 请赐教,如果这是合乎逻辑的?

编辑:位置回复

internal LocationRepo(IXmlReader xmlReader, IClassHelper classHelper)
{
    this.xmlReader = xmlReader;
    this.classHelper = classHelper;
    this.Init();
}

/// <summary>
/// Prevents a default instance of the <see cref="LocationRepo" /> class from being created.
/// </summary>
private LocationRepo() : this(new XmlReader(), new ClassHelper()){ }

1 个答案:

答案 0 :(得分:3)

接口没有构造函数,因此您不需要将参数传递给它,因此要创建模拟IClassHelper,只需执行此操作:< / p>

var mockClassHelper = new Mock<IClassHelper>();

这是使用界面的精彩世界。