Moq - It.IsAny() 始终返回 null
什么可能导致 It.IsAny
在每次调用时返回 null?我假设它被设计为返回非空字符串是错误的吗?
以下是用法 - Login 方法针对第二个参数(连接字符串)为空抛出 ArgumentNullException。我假设 It.IsAny
var mockApiHelper = new Mock<ApiHelper>();
mockApiHelper.Setup(m => m.Connect(It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>()));
var repositoryPlugin = new RepositoryPlugin(mockApiHelper.Object);
repositoryPlugin.Login(new CredentialsInfo(), It.IsAny<string>());
Assert.IsTrue(repositoryPlugin.LoggedIn,
"LoggedIn property should be true after the user logs in.");
What may cause It.IsAny<string>()
to return null at every call? Am I incorrect in assuming that it is designed to return a non-null string?
Here's the usage - where the Login method throws an ArgumentNullException for a null 2nd argument (connection string). I was assuming that It.IsAny<string>()
would provide a non-null string, which would bypass the ArgumentNullException.
var mockApiHelper = new Mock<ApiHelper>();
mockApiHelper.Setup(m => m.Connect(It.IsAny<string>(),
It.IsAny<string>(),
It.IsAny<string>()));
var repositoryPlugin = new RepositoryPlugin(mockApiHelper.Object);
repositoryPlugin.Login(new CredentialsInfo(), It.IsAny<string>());
Assert.IsTrue(repositoryPlugin.LoggedIn,
"LoggedIn property should be true after the user logs in.");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好吧,
It.IsAny
仅返回调用Match.Create
- 其中依次返回
default(TValue)
。对于任何引用类型来说,该值都将为 null。目前尚不清楚您是否真的在正确的对象上调用它 - 您不应该在模拟上调用它而不是在真实的代码上吗?
我见过的所有示例都在
mock.Setup
调用的上下文中使用It.IsAny
。您能否提供有关如何尝试使用它的更多信息?Well,
It.IsAny<TValue>
just returns the result of callingMatch<TValue>.Create
- which in turn returnsdefault(TValue)
. That will be null for any reference type.It's not clear whether you're really calling it on the right object though - shouldn't you be calling it on the mock rather than on the real code?
All the samples I've seen use
It.IsAny
in the context of amock.Setup
call. Could you give more information about how you're trying to use it?不,
It.IsAny
用于在您的设置中指定传递的任何字符串都将匹配。您可以进行设置,以便如果仅使用特定字符串调用您的方法,它将返回。考虑一下:无论使用模拟什么,都会根据调用 DoSomething 时传递模拟的参数获得不同的值。您可以在验证方法调用时执行相同的操作:
另外,我看到您编辑了您的问题。而不是:
这样做:
将时间更改为您期望的时间。如果您需要特定值,请将相关的
It.IsAny()
调用替换为这些实际值。No,
It.IsAny
is used to specify in your Setup that ANY string passed will match. You can do your setup so that if your method is called only with a particular string it will return. Consider this:Whatever is using the mock will then get different values depending on the parameter the mock is passed when DoSomething is invoked. You can do the same thing when verifying method calls:
Also, I see you edited your question. Instead of:
do this:
Change times to whatever you expect. If you expect particular values, replace the relevant
It.IsAny<string>()
calls with those actual values.It.IsAny
用于匹配Returns()
和Callback()
中的代码,控制推送到测试中的内容。It.IsAny
is used for matching its the code in yourReturns()
andCallback()
that control what gets pushed into your tests.