【问题标题】:Stub or Mock IMapper Returning Derived Classes where Base Expected存根或模拟 IMapper 返回预期基类的派生类
【发布时间】:2018-05-16 15:09:03
【问题描述】:

我有一个类在这样的构造函数中接受IMapper

public Foo(IMapper mapper)

在 Foo 的代码中我有这一行

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

BaseDAO 有 3 个子类型,在我这样设置的真实代码中

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

我想模拟上面的行

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

因此,如果传入Child1,则将返回Child1DAO,并且对于其他子类型也是如此。我试图将IMapper 存根,但以下方法返回一个错误,指出

Child1DAO 不能隐式转换为 Tdestination

我试图模拟出IMapper,但也无法让它发挥作用。

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

有什么想法吗?

【问题讨论】:

    标签: unit-testing mocking automapper moq stub


    【解决方案1】:

    出于本示例的目的,假设以下课程是被测对象

    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;
        }
    }
    

    IMapper 依赖项定义了以下合约

    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);
    
        //...
    }
    

    以下测试演示,使用最小起订量,

    模拟 IMapper 返回预期基类的派生类

    [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));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-15
      • 1970-01-01
      • 1970-01-01
      • 2019-08-14
      • 1970-01-01
      • 1970-01-01
      • 2019-11-04
      相关资源
      最近更新 更多