Could you please help me with fakeItEasy advanced usage. I got method (suppose it called "Insert") which takes an argument as parameter. This parameter filled with data (a lot of it). I want to return this argument as the result of "Insert" .
object.Insert(A<T>._))
.Invokes(obj =>
{
var p = obj.GetArgument<T>(0);
---SET p AS a return value here--
}
Something like that. Is it possible at all?
I tried
A.CallTo(() => obj.Insert(A<T>._)).ReturnsLazily((o) => o.GetArgument<T>(0);
It doesn't work too. In debug it didn't stop there. Probably it wasn't called at all.
Thanks in advance.
As Alexander Petrov says, this should work, but your code is too terse to tell for sure. I'll copy in the answer I'd already typed to you in the FakeItEasy gitter chat:
Your code is a little terse, so there are some details I have to guess at. It might be better to include more, and or/describe exactly what happens, but your second approach sounds good to me, although it's a little more complicated than it needs to be. I would've written this code myself:
A.CallTo(() => fake.Insert(A<TypeOfP>._).ReturnsLazily((TypeOfP p) => p));
Where TypeOfP
is the type of the argument. This is exactly the approach described at Return Values Calculated at Call Time.
If this is not working for you, the first thing I'd do is make sure you're configuring the right method. Are you using the correct type in the argument list? Is there an overload of Insert
that takes more (or fewer) parameters that you're actually calling in the production code, so the one-parameter version (that takes a TypeOfP
) isn't actually being called? Sometimes this is easy to check visually, or sometimes it can help to debug into the code.
If you're able to supply more of your test and production code, or at least method signatures, we might be able to help, if you're still stuck.