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 in IMatcher.Matches #1005

Closed
eduherminio opened this issue Apr 24, 2020 · 2 comments
Closed

NullReferenceException in IMatcher.Matches #1005

eduherminio opened this issue Apr 24, 2020 · 2 comments

Comments

@eduherminio
Copy link

I may have found a bug/issue while using Verify(Expression<Action<T>>, Times, string).

A suspicious NullReferenceException is triggered in Moq.Match.Moq.IMatcher.Matches(Object, Type).

I created a sample repo to easily reproduce this issue.

@stakx
Copy link
Contributor

stakx commented Apr 25, 2020

Seems to me this is your mistake, not a bug inside Moq.

In this line of code:

It.Is<TException>(e => e.GetHashCode() == ex.GetHashCode()),

you haven't considered the possibility that e might be null, so in such cases, e.GetHashCode() will throw a NullReferenceException.

This case appears to be triggered in your failing test:

_service.LogMessage(_level, _level.ToString());   // Logging a simple message
_logger.VerifyLog(_exception, _exception.Message, Times.Once());  // => NullReferenceException

You're logging a message, not an exception.

@eduherminio
Copy link
Author

That's it, nice catch!
The test was designed like that to trigger the mysterious exception.
It.Is<TException>(e => (e != null ? e.GetHashCode() : -1) == ex.GetHashCode()) does the trick

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

2 participants