Mock AutoMapper Mapper.Map call using Moq

You don’t need to mock AutoMapper, you can just inject the real one as explained here:

var myProfile = new MyProfile();
var configuration = new MapperConfiguration(cfg => cfg.AddProfile(myProfile));
IMapper mapper = new Mapper(configuration);

You can inject this mapper in your unit tests. The whole point of using tools like AutoMapper is for you not having to write a lot of mapping code. If you mock AutoMapper you’ll end up having to do that.

Leave a Comment