如何起订量



尝试对构造函数接受 Func 的类进行单元测试。不知道如何使用最小起订量模拟它。

public class FooBar
{
    public FooBar(Func<IFooBarProxy> fooBarProxyFactory)
    {
        _fooBarProxyFactory = fooBarProxyFactory;
    }
}

[Test]
public void A_Unit_Test()
{
    var nope = new Mock<Func<IFooBarProxy>>();
    var nope2 = new Func<Mock<IFooBarProxy>>();
    var fooBar = new FooBar(nope.Object);
    var fooBar2 = new FooBar(nope2.Object);
    // what's the syntax???
}

通了

public interface IFooBarProxy
{
    int DoProxyStuff();
}
public class FooBar
{
    private Func<IFooBarProxy> _fooBarProxyFactory;
    public FooBar(Func<IFooBarProxy> fooBarProxyFactory)
    {
        _fooBarProxyFactory = fooBarProxyFactory;
    }
    public int DoStuff()
    {
        var newProxy = _fooBarProxyFactory();
        return newProxy.DoProxyStuff();
    }
}
[TestFixture]
public class Fixture
{
    [Test]
    public void A_Unit_Test()
    {
        Func<IFooBarProxy> funcFooBarProxy = () =>
        {
            var mock = new Mock<IFooBarProxy>();
            mock.Setup(x => x.DoProxyStuff()).Returns(2);
            return mock.Object;
        };
        var fooBar = new FooBar(funcFooBarProxy);
        var result = fooBar.DoStuff();
        Assert.AreEqual(2, result);
    }
}

相关内容

  • 没有找到相关文章

最新更新