0

Currently I have something like this :-

    var someclass1 = JsonConvert.Deserialize<SomeClass1>(RandomData);
    var someclass2 = AutoMapper.Mapper.Map<Models.SomeClass2.SomeClass2>(someclass1);

    Assert.Equal(someclass2.Count, 1);

I know in order to do this I need to create a mock for IMappingEngine, but how do I mock the mapping above ?

Sharthak Ghosh
  • 576
  • 1
  • 9
  • 22
  • 1
    Why mock at all? You can simply use AM in your test. – Lucian Bargaoanu Aug 27 '17 at 10:06
  • its not working that way, I have tried out multiple solutions, my mappings are disposed off randomly and get wierd test results. That is why I wanted to mock it. Do u have any idea how we can do it in nsubstitute ? Ofcourse I know I can simply use AM. – Sharthak Ghosh Aug 27 '17 at 10:45
  • 1
    `its not working that way, I have tried out multiple solutions, my mappings are disposed off randomly and get wierd test results.` Can you show us that code, and the weird results of which you speak? – mjwills Aug 27 '17 at 11:33
  • its corporate code cant show, but what happens is that whenever I try to map using Mapper.Map, it throws a mapping exception even if I load the profiles correctly. Sometimes the very same tests pass too. So that is why I asked whether I could mock the mapping instead of depending on Mapper.Map. – Sharthak Ghosh Aug 27 '17 at 11:37
  • 1
    @SharthakGhosh , you are using Automapper in service locator pattern using the previous structure. AutoMapper also has the `IMapper` that can be injected into dependent classes and mocked if needed. Check their documentation – Nkosi Aug 27 '17 at 11:46
  • If AM works in your app, you should use it the same way in your tests. – Lucian Bargaoanu Aug 27 '17 at 15:46
  • If the mapper is a dependency for the class you are testing, mock it like `IMapper mapper = Substitute.For()`. If you are trying to do a maping test, build your `MapperConfiguration`, add the profile to it, act against it. – andrei.ciprian Nov 06 '18 at 12:54

0 Answers0