I am using the static version of AutoMapper. I have some Profile classes where in the constructor there are some dependencies, which in turn have dependencies of their own. So my question is since we set up AutoMapper once, how do I mock these dependencies, and should I even mock these in the first place, because I will use this for mapping actual objects.
-
1There is an article on Medium with the title 'How to read user claims from Automapper Value Resolver' and it'll help you with the issue. https://medium.com/@sergiobarriel/how-to-read-user-claims-from-automapper-value-resolver-1051d54235f2 – rsinis Sep 03 '18 at 21:28
1 Answers
It's hard to mock things when you use static, especially if you run tests in parallel. So the best thing you can do is to not use the static version of AutoMapper. We do use AutoMapper interface that we inject, that means we can always mock automapper itself.
As to whether you want to mock it or not, just imagine you will use same mapping in 5 different methods. Now you will have to write unit tests that will as well verify mapping was correct 5 times. Instead you could have verified that mapping was called and write unit tests for mapping itself directly against automapper.
It's up to you to decide whether the changes you need to make this possible is worth it. If you do a new design from scratch I can advise to make it like I described. A bit more writing, but gives you easy unit-testing and correct loose coupling.

- 27,817
- 27
- 121
- 207