Shortcut for "ignore all arguments" when using A.CallTo
See original GitHub issueFakeItEasy supplies the A<int>.Ignored
or A<int>._
helpers to easily ignore arguments when setting up a mock with A.CallTo
, but often I’m not interested in any of the arguments and this can get fairly ugly pretty fast, especially if the method takes generic types as arguments, like lists or dictionaries.
Is there any DSL abstraction that can be conjured up that just lets you ignore all parameters? So something that would instruct FakeItEasy to just supply A<T>.Ignored
for every argument. I’m trying to come up with something and it’s pretty tricky.
I’d love to see something like:
A.CallTo(() => myMock.MethodWithLotsOfArguments).Returns(true);
However, you can’t use a method group as the value of an expression.
It’s also not possible to define a ‘marker’ extension method on a method group, like so:
A.CallTo(() => myMock.MethodWithLotsOfArguments.IgnoreArguments()).Returns(true);
I thought maybe defining a delegate type, like IgnoreArgs
that lets you wrap the method group, like below, but that isn’t possible because you’d still have to specify the generic arguments on the delegate type, defeating the purpose. One possible feature of C# 6 was generic type inference on constructors, but that doesn’t help me now.
A.CallTo(() => new IgnoreArgs(myMock.MethodWithLotsOfArguments)).Returns(true);
The best option I am able to come up with is to just specify the method name as a string, bypassing the restrictions of method groups, but of course that has the downside of being very stringly typed…
A.CallTo(() => myMock.WithIgnoredArgs("MethodWithLotsOfArguments")).Returns(true);
Or some variation of that.
Any other ideas?
Issue Analytics
- State:
- Created 9 years ago
- Reactions:1
- Comments:34 (24 by maintainers)
Top GitHub Comments
@gdoron, that’s not possible, because the C# syntax doesn’t allow it. You can’t return a method group (except as a delegate, but we can’t have a
CallTo
overload for each possible method signature).I’ve thought about this as well, would be very nice to have a shorter way of ignoring all arguments. Is it not possible to extract the method name from a method group (leveraging generics)?
Imaginary syntax:
Making the stringly ‘where’ happen inside of fakeiteasy while still getting compile time errors?