模拟函数返回其输入时的TargetParameterCountException

时间:2018-06-26 10:45:45

标签: c# linq unit-testing moq

我有一个函数,需要模拟才能返回其参数之一。

该函数如下所示:

mockDocumentWrapper
    .Setup(m => m.WrapDocuments(It.IsAny<MessageSettings>(), It.IsAny<IEnumerable<XDocument>>()))
    .Returns((IEnumerable<XDocument> x) => x);

我这样嘲笑它:

WrapDocuments()

调用TargetParameterCountException的测试运行时,我得到commentFile.py

我在这里做什么错了?

1 个答案:

答案 0 :(得分:3)

Returns中使用的委托与设置中传递的参数数量不匹配。该方法需要2个参数,因此委托也应具有相同的参数。

mockDocumentWrapper
    .Setup(_ => _.WrapDocuments(It.IsAny<MessageSettings>(), It.IsAny<IEnumerable<XDocument>>()))
    .Returns((MessageSettings m, IEnumerable<XDocument> docs) => docs);