存根或模拟 IMapper 返回派生的 类,其中 Base Expected
Stub or Mock IMapper Returning Derived Classes where Base Expected
我有一个 class 在构造函数中接受一个 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
但下面的方法 returns 一个错误说
Child1DAO cannot be implicitly converted to a TDestination
我试着模仿 IMapper
但也无法让它工作。
public TDestination Map<TDestination>(object source)
{
return new Child1DAO();
}
有什么想法吗?
为了这个例子的目的,假设下面的class是被测对象
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);
//...
}
以下测试演示,使用最小起订量,
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));
}
}
我有一个 class 在构造函数中接受一个 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
但下面的方法 returns 一个错误说
Child1DAO cannot be implicitly converted to a TDestination
我试着模仿 IMapper
但也无法让它工作。
public TDestination Map<TDestination>(object source)
{
return new Child1DAO();
}
有什么想法吗?
为了这个例子的目的,假设下面的class是被测对象
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);
//...
}
以下测试演示,使用最小起订量,
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));
}
}