Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stubbing out parameters with pre-existing values in FakeItEasy

Tags:

c#

fakeiteasy

This is a bit of an odd one. I'm trying to stub a method which has out parameters, I don't care about what the parameters are so I'm ignoring the arguments. It looks like this:

List<Foo> ignored;
A.CallTo(() => fake.Method(out ignored))
  .Returns(something);

This works without any problems when the stubbed method is called like so:

List<Foo> target;
var result = service.Method(out target);

However, it doesn't work when the target is pre-initialised. For example:

List<Foo> target = new List<Foo>();
var result = service.Method(out target);

When I inspect the Tag on the fake, I can see that the out parameters are being recorded as <NULL> so I suspect they're not matching when the out target is already set to something. I've tried setting the ignored in my test to new List<Foo>() and also tried A<List<Foo>>.Ignored but neither has any effect on the result.

So my question is, does anyone know how to stub a method with out parameters if the out parameter target already has a value?

like image 754
James Gregory Avatar asked Jan 24 '12 23:01

James Gregory


1 Answers

Update: since FakeItEasy 1.23.0 the initial value of out parameters is ignored when matching, so no need for WithAnyArguments

, Five minutes later and I've found an acceptable solution (in this scenario). As I'm not interested in what arguments are passed to this method, so if I use the WithAnyArguments() method then it seems to work; this must shortcut the argument checking all together, I guess.

The final code is:

List<Foo> ignored;
A.CallTo(() => fake.Method(out ignored))
  .WithAnyArguments()
  .Returns(something);

This obviously doesn't solve the problem if I don't want to ignore all the arguments. I'll only accept this answer if nobody has a more sophisticated solution.

like image 91
James Gregory Avatar answered Nov 15 '22 10:11

James Gregory