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.");
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.IsAnyin the context of amock.Setupcall. Could you give more information about how you’re trying to use it?