question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Shortcut for "ignore all arguments" when using A.CallTo

See original GitHub issue

FakeItEasy 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:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:34 (24 by maintainers)

github_iconTop GitHub Comments

2reactions
thomaslevesquecommented, Apr 7, 2016

@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).

1reaction
Dashuecommented, Feb 4, 2016

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:

A.CallTo(() => myMock.MethodWithLotsOfArguments).WithAnyArguments().Returns(true);

Making the stringly ‘where’ happen inside of fakeiteasy while still getting compile time errors?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Argument constraints
The Ignored property can be used on any type. An underscore ( _ ) can be used as a shorthand for Ignored as...
Read more >
doCall: Executes a function call with option to ignore unused...
Executes a function call with option to ignore unused arguments. Usage. ## Default S3 method: doCall(.fcn, ..., args=NULL, alwaysArgs=NULL, .
Read more >
Shortcut to see method parameters, or placeholders?
I want to use Ctrl+Space while inside of a function call's parentheses in case I forget exactly what I need to pass there,...
Read more >
Advanced Patterns — Click Documentation (8.1.x)
Parameters (options and arguments) are forwarded to the command callbacks as you ... Unknown long options are generally ignored and not processed at...
Read more >
python - How to properly ignore exceptions
If you definitely want to ignore all errors, catch Exception rather than a bare except: statement. Again, why?
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found