0

I am using Automapper 6.2.1 and by that removed all statics and instead I am injecting IMapper.

I am using NSubstitute for mocking.

I have a bit of code where I map two existing objects.

public class Person1 {
        public string Value1 { get; set; }
        public string Value2 { get; set; }
}

public class Person2 {
        public string Value2 { get; set; }
}

...

_mapper.Map(person2, person1);

My mapping will replace Value2 in person1.

I am thereafter using person1 with the modified value.

Is it possible to "return" a different person1 from my mock? And how can I do that, if possible?

EDIT

Yes, my question is how I can mock my _mapper correctly and "return" a different person1 (by ref) using NSubstitute.

person1 is a reference object meaning that in the real implementation Value2 from person2 will replace Value2 in person1. But in my unit test I failed to simulate this scenario.

Per
  • 1,393
  • 16
  • 28
  • Is your problem that you don't know how to mock `Map` method? – AlbertK Feb 21 '18 at 09:43
  • I don't think there is enough information provided in the question to answer this. Please see the [Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve) documentation for help with this. :) – David Tchepak Feb 22 '18 at 05:35

1 Answers1

1

You can modify Person1 argument inside Do method if you don't use result parameter of Map (it will be null)

_mapper
    .When(x => x.Map(person2, person1))
    .Do(x =>
        {
            var person1Arg = x.Arg<Person1>();
            person1Arg.Value1 = "value-1";
            person1Arg.Value2 = "value-2";
        });

//map and use person1
_mapper.Map(person2, person1);

Or you can return new Person1 object from Map, but in this case original person1 was not modified

_mapper.Map(person2, person1)
    .Returns(new Person1
             {
                 Value1 = "value-1",
                 Value2 = "value-2"
             });

//then map and use resultPerson1 instead of person1
var resultPerson1 = _mapper.Map(person2, person1);
witalego
  • 551
  • 3
  • 7