Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullReferenceException when mocked method has a nullable value type argument, but It.IsAny constraint is not nullable #90

Closed
ashmind opened this issue Feb 7, 2014 · 3 comments

Comments

@ashmind
Copy link
Contributor

ashmind commented Feb 7, 2014

Example:

public interface ISomeInterface {
    void DoSomething(Guid? guid);
}

mock.Setup(x => x.DoSomething(It.IsAny<Guid>()))
mock.Object.DoSomething(null);

Result:

System.NullReferenceException : Object reference not set to an instance of an object.
    at Moq.Match`1.Matches(Object value)
    at Moq.MethodCall.Matches(ICallContext call)
    at System.Linq.Enumerable.LastOrDefault(IEnumerable`1 source, Func`2 predicate)
    at Moq.ExtractProxyCall.HandleIntercept(ICallContext invocation, InterceptStrategyContext ctx)
    at Moq.Interceptor.Intercept(ICallContext invocation)
    at Castle.DynamicProxy.AbstractInvocation.Proceed()
    at Castle.Proxies.ISomeInterfaceProxy.DoSomething(Nullable`1 guid)
@anonymous112233
Copy link

This seem to back as of latest v. 4.2.1506.2016

@yrshaikh
Copy link

For those who are coming here in 2019. Try this

mock.Setup(x => x.DoSomething(It.IsAny<Guid?>()))

@stakx
Copy link
Contributor

stakx commented Aug 26, 2019

@yrshaikh: I'm mentioning the following just in case that there is a misunderstanding here: The difference between It.IsAny<Guid>() or It.IsAny<Guid?>() lies in whether or not you want to match null as well. In up-to-date versions of Moq, you should no longer see a NullReferenceException (which is what this issue was about) regardless of which form you choose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants