4

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

xeraphim
  • 4,375
  • 9
  • 54
  • 102

2 Answers2

4

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);
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
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 :-)

xeraphim
  • 4,375
  • 9
  • 54
  • 102