gpt4 book ai didi

.net - 如何起订功能

转载 作者:行者123 更新时间:2023-12-04 07:44:04 25 4
gpt4 key购买 nike

尝试对构造函数采用Func的类进行单元测试。不确定如何使用Moq模拟它。

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

关于.net - 如何起订功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6036708/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com