FakeItEasy return object that the method is called with
Asked Answered
M

2

5

I want to setup my fake like this:

A.CallTo(() => this.repository.Create(A<PersonModel>._)).Returns(XYZ);

where XYZ is the same variable as was inserted at A<PersonModel>._

so if Create is called with mySamplePersonModel I want the method to return mySamplePersonModel.

How can I achieve this?

Thanks in advance

Mila answered 26/9, 2017 at 10:23 Comment(0)
R
5

The solution you found is correct. There's an alternative that is a little more readable IMO:

A.CallTo(() => repository.Create(A<PersonModel>._)).ReturnsLazily((PersonModel p) => p);
Rationalism answered 26/9, 2017 at 12:48 Comment(0)
M
3

I found the answer you can capture arguments like this:

A.CallTo(() => this.repository.Create(A<PersonModel>._)).ReturnsLazily(x => x.Arguments.Get<PersonModel>(0));

And you can even modify this parameter like this:

A.CallTo(() => this.repository.Create(A<PersonModel>._)).ReturnsLazily(x =>
            {
                var personModel = x.Arguments.Get<PersonModel>(0);
                personModel.Name = "aName";
                return personModel;
            });

If anyone has a more elegant solution, feel free to post it :-)

Mila answered 26/9, 2017 at 10:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.