[英]Moq converts It.IsAny<Exception> to It.IsAny<string> in expectation
I'm using Moq for unit tests, and I've set up an expectation like this: 我正在使用Moq进行单元测试,并且已经设定了如下期望:
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: 这样可以构建并正常运行,但是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)
So it's changing the Exception to a string for some reason.... 因此出于某种原因将Exception更改为字符串。
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! 谢谢!
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! 我没有将myMock.Object传递给调用命令!
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. 好的,所以我创建了一个测试方法,该方法具有一个异常作为参数,并以上述方式使用moq进行了调用,并且效果很好。 So it doesn't seem to be a problem with passing an Exception as a parameter per se.
因此,将Exception作为参数本身传递似乎并不成问题。
I also changed the first parameter from an enum value to an It.IsAny enum. 我还将第一个参数从枚举值更改为It.IsAny枚举。 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..... 因此,由于某种原因,它似乎正在采用第一个参数的类型并将其应用于所有其余参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.