Search code examples
unit-testingmockingautomappermoqstub

Stub or Mock IMapper Returning Derived Classes where Base Expected


I have a class that takes in an IMapper in the constructor like this

public Foo(IMapper mapper)

In the code for Foo I have this line

var dao = _mapper.Map<BaseDAO>(obj);

BaseDAO has 3 subtypes that in the real code I have set up like this

CreateMap<Base, BaseDAO>()
     .Include<Child1, Child1DAO>()
     .Include<Child2, Child2DAO>()
     .Include<Child3, Child3DAO>();

I would like to mock out the above line

var dao = _mapper.Map<BaseDAO>(obj);

so that if a Child1 is passed in then a Child1DAO will be returned and the same for the other subtypes. I tried to stub out IMapper but the following method returns an error saying that

Child1DAO cannot be implicitly converted to a TDestination

and I tried to mock out IMapper but could not get that to work either.

public TDestination Map<TDestination>(object source)
{
    return new Child1DAO();
}

Any ideas?


Solution

  • For the purposes of this example, assume the following class is the subject under test

    public class Foo {
        private IMapper mapper;
        public Foo(IMapper mapper) {
            this.mapper = mapper;
        }
    
        public BaseDAO Bar(object obj) {
            var dao = mapper.Map<BaseDAO>(obj);
            return dao;
        }
    }
    

    Where the IMapper dependency has the following contract defined

    public interface IMapper {
        /// <summary>
        /// Execute a mapping from the source object to a new destination object.
        /// The source type is inferred from the source object.
        /// </summary>
        /// <typeparam name="TDestination">Destination type to create</typeparam>
        /// <param name="source">Source object to map from</param>
        /// <returns>Mapped destination object</returns>
        TDestination Map<TDestination>(object source);
    
        //...
    }
    

    The following test demonstrates, using moq,

    Mock IMapper Returning Derived Classes where Base Expected

    [TestClass]
    public class TestClass {
        [TestMethod]
        public void _TestMethod() {
            //Arrange
            var mock = new Mock<IMapper>();
            var foo = new Foo(mock.Object);
    
            mock
                //setup the mocked function
                .Setup(_ => _.Map<BaseDAO>(It.IsAny<object>()))
                //fake/stub what mocked function should return given provided arg
                .Returns((object arg) => {
                    if (arg != null && arg is Child1)
                        return new Child1DAO();
                    if (arg != null && arg is Child2)
                        return new Child2DAO();
                    if (arg != null && arg is Child3)
                        return new Child3DAO();
    
                    return null;
                });
    
            var child1 = new Child1();
    
            //Act
            var actual = foo.Bar(child1);
    
            //Assert
            Assert.IsNotNull(actual);
            Assert.IsInstanceOfType(actual, typeof(BaseDAO));
            Assert.IsInstanceOfType(actual, typeof(Child1DAO));
        }
    }