Is it correct to have constructor dependencies inside an AutoMapper Profile class, can it cause problems during testing?

138 Views Asked by At

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.

1

There are 1 best solutions below

0
Ilya Chernomordik On

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.