Moq 将 It.IsAny转换为 到 It.IsAny<字符串>; 在期待中

发布于 2024-07-30 14:27:41 字数 872 浏览 3 评论 0原文

我正在使用 Moq 进行单元测试,并且我设置了这样的期望:

myMock.Expect(w => w.MyMethod(It.IsAny<string>(),
                              It.IsAny<string>(),
                              It.IsAny<string>(),
                              It.IsAny<System.Exception>(), null))
      .Returns(myResult);

它正在模拟的方法是:

logger.WriteLogItem(string1, string2, string3, System.Exception, IEnumerableInstantiation);

此构建并运行良好,但是VerifyAll() 没有通过,我得到的错误是:

Moq.MockVerificationException : The following expectations were not met:
IMyClass l => l.MyMethod(It.IsAny<string>(), It.IsAny<string>(), 
                         It.IsAny<string>(), It.IsAny<String>(), null)

所以它是由于某种原因将异常更改为字符串......

有没有人以前见过这个/知道它为什么这样做以及我可以做些什么来修复它/解决它?

谢谢!

I'm using Moq for unit tests, and I've set up an expectation like this:

myMock.Expect(w => w.MyMethod(It.IsAny<string>(),
                              It.IsAny<string>(),
                              It.IsAny<string>(),
                              It.IsAny<System.Exception>(), null))
      .Returns(myResult);

the method it is mocking is:

logger.WriteLogItem(string1, string2, string3, System.Exception, IEnumerableInstantiation);

This builds and runs fine, however VerifyAll() does not pass, and the error I get is:

Moq.MockVerificationException : The following expectations were not met:
IMyClass l => l.MyMethod(It.IsAny<string>(), It.IsAny<string>(), 
                         It.IsAny<string>(), It.IsAny<String>(), null)

So it's changing the Exception to a string for some reason....

Has anyone seen this before/ have any idea why it's doing this and what I can do to fix it/work around it?

thanks!

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

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

发布评论

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

评论(2

梦毁影碎の 2024-08-06 14:27:41

啊,这是一个菜鸟错误! 转换的事情只是一个转移注意力的事情,旨在让我在互联网上追逐寻找不存在的疯狂答案。

我没有将 myMock.Object 传递给调用命令!

Ah, it was a rookie error! And the conversion thing was just a red-herring designed to send me chasing round the internet looking for crazy answers that aren't out there.

I wasn't passing the myMock.Object through to the calling command!

花开浅夏 2024-08-06 14:27:41

好的,所以我创建了一个测试方法,该方法有一个异常作为参数,并以上面的方式使用最小起订量调用该方法,并且效果很好。 因此,将 Exception 作为参数本身传递似乎不是问题。

我还将第一个参数从枚举值更改为 It.IsAny 枚举。 所以从

myMock.Expect(w =>
    w.MyMethod(**MyEnum.Value**,
        It.IsAny<string>(),
        It.IsAny<string>(),
        It.IsAny<System.Exception>(),
        null))
.Returns(myResult);

myMock.Expect(w =>
    w.MyMethod(**It.IsAny<MyEnum>()**,        
        It.IsAny<string>(),
        It.IsAny<string>(),
        It.IsAny<System.Exception>(),
        null))
.Returns(myResult);

我得到的输出是:

IMyClass l =>
    l.MyMethod(IsAny<MyEnum>(),
        IsAny<MyEnum>(), 
        IsAny<MyEnum>(),
        IsAny<MyEnum>(),
        null)

所以看起来它正在采用第一个参数的类型,并出于某种原因将其应用于所有其余参数......

Okay, so I created a test method that had an exception as a parameter and called that using moq in the way above, and it worked fine. So it doesn't seem to be a problem with passing an Exception as a parameter per se.

I also changed the first parameter from an enum value to an It.IsAny enum. So from

myMock.Expect(w =>
    w.MyMethod(**MyEnum.Value**,
        It.IsAny<string>(),
        It.IsAny<string>(),
        It.IsAny<System.Exception>(),
        null))
.Returns(myResult);

to

myMock.Expect(w =>
    w.MyMethod(**It.IsAny<MyEnum>()**,        
        It.IsAny<string>(),
        It.IsAny<string>(),
        It.IsAny<System.Exception>(),
        null))
.Returns(myResult);

and the output I got was:

IMyClass l =>
    l.MyMethod(IsAny<MyEnum>(),
        IsAny<MyEnum>(), 
        IsAny<MyEnum>(),
        IsAny<MyEnum>(),
        null)

So it looks like it's taking the type of the first parameter and applying it to all the rest for some reason.....

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