Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing Stub或Mock-IMapper返回需要基类的派生类_Unit Testing_Mocking_Automapper_Moq_Stub - Fatal编程技术网

Unit testing Stub或Mock-IMapper返回需要基类的派生类

Unit testing Stub或Mock-IMapper返回需要基类的派生类,unit-testing,mocking,automapper,moq,stub,Unit Testing,Mocking,Automapper,Moq,Stub,我有一个类,它在构造函数中接受一个IMapper,如下所示 public Foo(IMapper mapper) CreateMap<Base, BaseDAO>() .Include<Child1, Child1DAO>() .Include<Child2, Child2DAO>() .Include<Child3, Child3DAO>(); 在Foo的代码中,我有一行 var dao = _mapper.Ma

我有一个类,它在构造函数中接受一个
IMapper
,如下所示

public Foo(IMapper mapper)
CreateMap<Base, BaseDAO>()
     .Include<Child1, Child1DAO>()
     .Include<Child2, Child2DAO>()
     .Include<Child3, Child3DAO>();
在Foo的代码中,我有一行

var dao = _mapper.Map<BaseDAO>(obj);
我想模拟一下上面这一行

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

有什么想法吗?

在本例中,假设下面的类是被测试的主题

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;
    }
}
以下试验证明,使用最小起重量

Mock IMapper返回所需基类的派生类

[TestClass]
公共类TestClass{
[测试方法]
public void_TestMethod(){
//安排
var mock=new mock();
var foo=新foo(mock.Object);
嘲弄
//设置模拟函数
.Setup(=>u.Map(It.IsAny()))
//fake/stub给定提供的参数,模拟函数应该返回什么
.返回((对象参数)=>{
如果(arg!=null&&arg为Child1)
返回新的Child1DAO();
如果(arg!=null&&arg为Child2)
返回新的Child2DAO();
如果(arg!=null&&arg为Child3)
返回新的Child3DAO();
返回null;
});
var child1=新的child1();
//表演
var实际值=foo.Bar(child1);
//断言
Assert.IsNotNull(实际值);
Assert.IsInstanceOfType(实际,typeof(BaseDAO));
Assert.IsInstanceOfType(实际,typeof(Child1DAO));
}
}
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;
    }
}
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);

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