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

Please document generic argument matching support for versions prior to 4.13 #1119

Closed
akorczynskikcura opened this issue Dec 21, 2020 · 3 comments

Comments

@akorczynskikcura
Copy link

akorczynskikcura commented Dec 21, 2020

We cannot upgrade to newer versions due to some incompatibility issue with Castle dependency. Please document how to match generic arguments prior to v4.13. Whether it is even possible to do something equivalent to

public interface IFoo
{
    bool M1<T>();
    bool M2<T>(T arg);
}

var mock = new Mock<IFoo>();
// matches any type argument:
mock.Setup(m => m.M1<It.IsAnyType>()).Returns(true);
@stakx
Copy link
Contributor

stakx commented Dec 21, 2020

Please document [...]

Feel free to help out... this is an open-source project, not a free support service. 😉

That being said, back to your actual question: Note that It.IsAnyType is not actually needed in a many simple cases. Often, you can just use object as a type argument when setting up a method; since all values in .NET are convertible to object, that might suffice to match anything.

It.IsAnyType or other type matchers only need to be used where object doesn't work (off the top of my head, e.g. when a generic type parameter constraint disallows object, or with nested types where co-/contravariance might get in the way).

@akorczynskikcura
Copy link
Author

Thank you. There you go, I contributed that to quickstart page ;) Feel free to update if needed.

@stakx
Copy link
Contributor

stakx commented Dec 22, 2020

Looks great, thanks @akorczynskikcura! 👍

I did make a few minor changes, most notably moving the object part before the It.IsAnyType part, first because it will work for older Moq versions, too, and more importantly, because it's the simpler & better performing solution and should be tried first.

Thanks for contributing!

@stakx stakx closed this as completed Dec 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants