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

Using type matchers with .Callback and .Returns should be forbidden #1205

Closed
stakx opened this issue Aug 25, 2021 · 0 comments · Fixed by #1206
Closed

Using type matchers with .Callback and .Returns should be forbidden #1205

stakx opened this issue Aug 25, 2021 · 0 comments · Fixed by #1206
Assignees
Milestone

Comments

@stakx
Copy link
Contributor

stakx commented Aug 25, 2021

This is a follow-up to #1137.

interface IX
{
    void Method<T>(T arg);
}

var mock = new Mock<IX>();
mock.Setup(m => m.Method(It.IsAny<It.IsAnyType>()))
    .Callback<It.IsAnyType>(arg => ...);

While all static type checking will check out fine, there's still an error here: at runtime, the callback's arg will never have type It.IsAnyType—it should really be typed object. Otherwise the callback will throw an ArgumentException ("Object of type '...' cannot be converted to type 'Moq.It+IsAnyType'.").

I believe that any use of type matchers with setup callbacks should be forbidden to catch that kind of mistake early.

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

Successfully merging a pull request may close this issue.

1 participant