【问题标题】:Moq NUnit test System.Reflection.TargetParameterCountException : Parameter count mismatchMoq NUnit 测试 System.Reflection.TargetParameterCountException:参数计数不匹配
【发布时间】:2016-09-21 12:56:34
【问题描述】:

我有一个界面:

public interface IConfig
{
    // other methods...
    T GetEnum<T>(string name, bool ignoreCase = false, T @default = default(T)) where T : struct;
}

我的 SUT 使用什么:

public void DoSomething(IConfig config)
{
    var enumVal = config.GetEnum("MyEnum", false, MyEnum.A));
}

我的测试起订量是:

var configMock = new Mock<IConfig>();
configMock.Setup(r => r.GetEnum("MyEnum", It.IsAny<bool>(), It.IsAny<MyEnum>())).Returns<MyEnum>(r => MyEnum.B);
sut.DoSomething(configMock.Object);

当测试运行时,它会以System.Reflection.TargetParameterCountException : Parameter count mismatch. 失败

如果我指定 Returns 子句并允许它返回默认值,则模拟可以工作,那么问题似乎就在那里?

【问题讨论】:

    标签: c# unit-testing nunit moq


    【解决方案1】:

    我通过替换让它运行

    Returns<MyEnum>(r => MyEnum.B);
    

    .Returns(MyEnum.B);
    

    【讨论】:

    • 当我尝试时,如果我尝试了编译器错误,这意味着我的净化示例没有正确反映我的实际代码。错误是:Argument 1: cannot convert from 'MyEnum' to 'System.Func&lt;MyEnum, MyEnum&gt;'
    • 您是否解决了您发布的内容与实际代码之间的差异?
    • 很遗憾没有;希望我能找到更多时间来追踪这个!
    猜你喜欢
    • 2011-12-04
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    • 2022-12-08
    • 2011-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多