Moq 将 It.IsAny转换为 到 It.IsAny<字符串>; 在期待中字符串>
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
啊,这是一个菜鸟错误! 转换的事情只是一个转移注意力的事情,旨在让我在互联网上追逐寻找不存在的疯狂答案。
我没有将 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!
好的,所以我创建了一个测试方法,该方法有一个异常作为参数,并以上面的方式使用最小起订量调用该方法,并且效果很好。 因此,将 Exception 作为参数本身传递似乎不是问题。
我还将第一个参数从枚举值更改为 It.IsAny 枚举。 所以从
到
我得到的输出是:
所以看起来它正在采用第一个参数的类型,并出于某种原因将其应用于所有其余参数......
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
to
and the output I got was:
So it looks like it's taking the type of the first parameter and applying it to all the rest for some reason.....