There is such a code base where we use automapper and have 2 layers, Domainand Service. Each object has its own object for presenting data, DomainItemand ServiceItem. The service receives data from the domain, uses a constructor that injects an instance of automapper to display
class Service
{
public ServiceItem Get(int id)
{
var domainItem = this.domain.Get(id);
return this.mapper.Map<DomainItem, ServiceItem>(domainItem);
}
}
Assume best practices, so mapper has no side effects and no external dependencies. You have to write a static function to convert one object to another in a few seconds, just matching fields.
With that in mind, is it good practice to mock the linker in unit tests?
[TestClass]
class UnitTests
{
[TestMethod]
public void Test()
{
var expected = new ServiceItem();
var mockDomain = new Mock<IDomain>();
var mockMapper = new Mock<IMapper>();
mockMapper.Setup(x => x.Map<DomainItem, ServiceItem>(It.IsAny<DomainItem>()))
.Returns(expected);
var service = new Service(mockDomain.Object, mockMapper.Object);
var result = service.Get(0);
Assert.AreEqual(expected, result);
}
}
, unit test , mocks, mapper, . - ?