Moq - It.IsAny() 始终返回 null

发布于 2024-11-08 19:54:47 字数 740 浏览 0 评论 0原文

什么可能导致 It.IsAny() 在每次调用时返回 null?我假设它被设计为返回非空字符串是错误的吗?

以下是用法 - Login 方法针对第二个参数(连接字符串)为空抛出 ArgumentNullException。我假设 It.IsAny() 将提供一个非空字符串,这将绕过 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.");

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 技术交流群。

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

发布评论

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

评论(3

残月升风 2024-11-15 19:54:47

好吧,It.IsAny仅返回调用 Match.Create- 其中依次返回default(TValue)。对于任何引用类型来说,该值都将为 null。

目前尚不清楚您是否真的在正确的对象上调用它 - 您不应该在模拟上调用它而不是在真实的代码上吗?

我见过的所有示例都在 mock.Setup 调用的上下文中使用 It.IsAny。您能否提供有关如何尝试使用它的更多信息?

Well, It.IsAny<TValue> just returns the result of calling Match<TValue>.Create - which in turn returns default(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 a mock.Setup call. Could you give more information about how you're trying to use it?

如若梦似彩虹 2024-11-15 19:54:47

不,It.IsAny 用于在您的设置中指定传递的任何字符串都将匹配。您可以进行设置,以便如果仅使用特定字符串调用您的方法,它将返回。考虑一下:

myMock.Setup(x => x.DoSomething(It.IsAny<string>()).Return(123);
myMock.Setup(x => x.DoSomething("SpecialString").Return(456);

无论使用模拟什么,都会根据调用 DoSomething 时传递模拟的参数获得不同的值。您可以在验证方法调用时执行相同的操作:

myMock.Verify(x => x.DoSomething(It.IsAny<string>())); // As long as DoSomething was called, this will be fine.
myMock.Verify(x => x.DoSomething("SpecialString"));  // DoSomething MUST have been called with "SpecialString"

另外,我看到您编辑了您的问题。而不是:

Assert.IsTrue(repositoryPlugin.LoggedIn, 
    "LoggedIn property should be true after the user logs in.");

这样做:

mockApiHelper.Verify( x => 
   x.Connect(It.IsAny<string>(), It.IsAny<string>(), 
       It.IsAny<string>()), Times.Once()); 

将时间更改为您期望的时间。如果您需要特定值,请将相关的 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:

myMock.Setup(x => x.DoSomething(It.IsAny<string>()).Return(123);
myMock.Setup(x => x.DoSomething("SpecialString").Return(456);

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:

myMock.Verify(x => x.DoSomething(It.IsAny<string>())); // As long as DoSomething was called, this will be fine.
myMock.Verify(x => x.DoSomething("SpecialString"));  // DoSomething MUST have been called with "SpecialString"

Also, I see you edited your question. Instead of:

Assert.IsTrue(repositoryPlugin.LoggedIn, 
    "LoggedIn property should be true after the user logs in.");

do this:

mockApiHelper.Verify( x => 
   x.Connect(It.IsAny<string>(), It.IsAny<string>(), 
       It.IsAny<string>()), Times.Once()); 

Change times to whatever you expect. If you expect particular values, replace the relevant It.IsAny<string>() calls with those actual values.

尬尬 2024-11-15 19:54:47

It.IsAny 用于匹配 Returns()Callback() 中的代码,控制推送到测试中的内容。

It.IsAny is used for matching its the code in your Returns() and Callback() that control what gets pushed into your tests.

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