起订量 +单元测试 - System.Reflection.TargetParameterCountException:参数计数不匹配

发布于 2024-12-09 05:02:09 字数 583 浏览 0 评论 0 原文

我尝试使用带有多个参数函数的 lambda,但当我尝试调用 mock.Object.Convert(value, null, null, null); 行时,Moq 在运行时抛出此异常。

System.Reflection.TargetParameterCountException: Parameter count mismatch

代码是:

var mock = new Mock<IValueConverter>();

mock.Setup(conv => conv.Convert(It.IsAny<Object>(), It.IsAny<Type>(),
    It.IsAny<Object>(), It.IsAny<CultureInfo>())).Returns((Int32 num) => num + 5);

var value = 5;
var expected = 10;
var actual = mock.Object.Convert(value, null, null, null);

实现它的正确方法是什么?

I'm tring to use a lambda with a multiple-params function but Moq throws this exception at runtime when I attempt to call the mock.Object.Convert(value, null, null, null); line.

System.Reflection.TargetParameterCountException: Parameter count mismatch

The code is:

var mock = new Mock<IValueConverter>();

mock.Setup(conv => conv.Convert(It.IsAny<Object>(), It.IsAny<Type>(),
    It.IsAny<Object>(), It.IsAny<CultureInfo>())).Returns((Int32 num) => num + 5);

var value = 5;
var expected = 10;
var actual = mock.Object.Convert(value, null, null, null);

What is the proper way to implement it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沫离伤花 2024-12-16 05:02:09

这是您的 Returns 子句。您正在设置一个 4 参数方法,但仅使用 1 参数 lambda。我运行以下命令没有问题:

[TestMethod]
public void IValueConverter()
{
    var myStub = new Mock<IValueConverter>();
    myStub.Setup(conv => conv.Convert(It.IsAny<object>(), It.IsAny<Type>(), It.IsAny<object>(), It.IsAny<CultureInfo>())).
        Returns((object one, Type two, object three, CultureInfo four) => (int)one + 5);

    var value = 5;
    var expected = 10;

    var actual = myStub.Object.Convert(value, null, null, null);

    Assert.AreEqual<int>(expected, (int) actual);
}

没有异常,测试通过。

It's your Returns clause. You have a 4 parameter method that you're setting up, but you're only using a 1 parameter lambda. I ran the following without issue:

[TestMethod]
public void IValueConverter()
{
    var myStub = new Mock<IValueConverter>();
    myStub.Setup(conv => conv.Convert(It.IsAny<object>(), It.IsAny<Type>(), It.IsAny<object>(), It.IsAny<CultureInfo>())).
        Returns((object one, Type two, object three, CultureInfo four) => (int)one + 5);

    var value = 5;
    var expected = 10;

    var actual = myStub.Object.Convert(value, null, null, null);

    Assert.AreEqual<int>(expected, (int) actual);
}

No exceptions, test passed.

半窗疏影 2024-12-16 05:02:09

这不是OP的答案,但也许是未来的googlers的答案:

我有一个Callback,它与正在设置的方法的签名不匹配

Mock
    .Setup(r => r.GetNextCustomerNumber(It.IsAny<int>()))
    .Returns(AccountCounter++)
    .Callback<string, int>(badStringParam, leadingDigit =>
    {
        // Doing stuff here, note that the 'GetNextCustomerNumber' signature is a single int 
        // but the callback unreasonably expects an additional string parameter.
    });

这是一些重构的结果,重构工具当然不能意识到Callback签名不正确

Not an answer for OP but perhaps for future googlers:

I had a Callback that didn't match the signature of the method being setup

Mock
    .Setup(r => r.GetNextCustomerNumber(It.IsAny<int>()))
    .Returns(AccountCounter++)
    .Callback<string, int>(badStringParam, leadingDigit =>
    {
        // Doing stuff here, note that the 'GetNextCustomerNumber' signature is a single int 
        // but the callback unreasonably expects an additional string parameter.
    });

This was the result of some refactoring and the refactoring tool of course couldn't realise that the Callback signature was incorrect

樱桃奶球 2024-12-16 05:02:09

也许是因为您传递的是 nullIt.IsAny() 需要除 null 之外的任何 object ?如果你执行以下操作会发生什么?:

var actual = mock.Object.Convert(value, new object(), typeof(object), CultureInfo.CurrentCulture);

这只是我在黑暗中的一刺,我更熟悉Rhino.Mocks。


我的第二个猜测:

查看了下载附带的 Moq.chm 后,

您正在使用 Setup(Expression>) 方法,该方法“为模拟类型指定设置”对 void 方法的调用。”

您需要 Setup(Expression>) 方法“为调用值返回方法指定模拟类型的设置”。

所以你可以尝试:

mock.Setup<Int32>(
    conv => {
        conv.Convert(
            It.IsAny<Object>(), 
            It.IsAny<Type>(),
            It.IsAny<Object>(), 
            It.IsAny<CultureInfo>());
        return  num + 5;
        });

Perhaps it's because you are passing null but It.IsAny<Object>() is expecting any object except null? What happens if you do the following?:

var actual = mock.Object.Convert(value, new object(), typeof(object), CultureInfo.CurrentCulture);

This is just a stab in the dark from me, I'm more familiar with Rhino.Mocks.


My 2nd guess:

Having looked at the Moq.chm that comes with the download,

You are using the Setup(Expression<Action<T>>) method which "Specifies a setup on the mocked type for a call to a void method."

You want te Setup<TResult>(Expression<Func<T,TResult>>) method that "Specifies a setup on the mocked type for a call to a value returning method".

So you could try:

mock.Setup<Int32>(
    conv => {
        conv.Convert(
            It.IsAny<Object>(), 
            It.IsAny<Type>(),
            It.IsAny<Object>(), 
            It.IsAny<CultureInfo>());
        return  num + 5;
        });
骄兵必败 2024-12-16 05:02:09

就我而言,我认为 Returns 中的类型是输出类型,但实际上它是输入类型。

因此,如果您有一个方法,

public virtual string Foo(int a, int b) { ... }

正确的子句是 .Returns(...),而不是 .Returns(...) 这是我最初是这么想的。

我的错误是因为我最初测试的是具有相同输入和返回类型的函数 - 例如 public virtual string Foo(string a)

In my case, I thought that the type in Returns<> is the output type, but in fact it was the input type(s).

So if you have a method

public virtual string Foo(int a, int b) { ... }

The correct clause is .Returns<int, int>(...), NOT .Returns<string>(...) which is what I thought initially.

My mistake was because I was testing a function with the same input and return type initially - for example public virtual string Foo(string a).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文