gpt4 book ai didi

c# - AutoFixture+Moq - 卡住模拟类以防止设置

转载 作者:太空狗 更新时间:2023-10-29 23:29:03 26 4
gpt4 key购买 nike

我想了解为什么当我卡住模拟类而不是模拟接口(interface)时我的测试失败

[Fact]
public void MethodeName()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());

var webRequestMock = fixture.Freeze<Mock<MyWebRequest>>(); // freezing a class + setuping a return value
webRequestMock.Setup(a => a.GetData())
.Returns("Foo");

var myService = fixture.Create<MyService>();

var actual = myService.GetData();

webRequestMock.Verify(a => a.GetData(), Times.Once()); // Failed it's never called once
Assert.Equal("Foo", actual); // Failed, if we comment previous line, 'actual' value is always empty
}

public class MyService
{
private readonly MyWebRequest _request;

public MyService(MyWebRequest request)
{
_request = request;
}

public string GetData()
{
var data = _request.GetData();
return data;
}
}

public class MyWebRequest
{
public virtual string GetData() // you can see here, the method is well virtual, and should be overridable by moq.
{
return string.Empty;
}
}

1/ 如果我尝试使用模拟界面,它会起作用。

[Fact]
public void MethodeName()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
const string expected = "Foo";

var webRequestMock = fixture.Freeze<Mock<IMyWebRequest>>();
webRequestMock.Setup(a => a.GetData())
.Returns("Foo");

var myService = fixture.Create<MyService>();

var actual = myService.GetData();

webRequestMock.Verify(a => a.GetData(), Times.Once()); // Success
Assert.Equal(expected, actual); // Success
}

public class MyService
{
private readonly IMyWebRequest _request;

public MyService(IMyWebRequest request)
{
_request = request;
}

public string GetData()
{
var data = _request.GetData();
return data;
}
}

public interface IMyWebRequest
{
string GetData();
}

public class MyWebRequest : IMyWebRequest
{
public virtual string GetData()
{
return string.Empty;
}
}

2/ 如果我使用“注入(inject)”而不是卡住,它会起作用:

[Fact]
public void MethodeName()
{
var fixture = new Fixture().Customize(new AutoMoqCustomization());
const string expected = "Foo";

var webRequestMock = new Mock<MyWebRequest>();
webRequestMock.Setup(a => a.GetData()).Returns("Foo");
fixture.Inject(webRequestMock.Object);

var myService = fixture.Create<MyService>();

var actual = myService.GetData();

webRequestMock.Verify(a => a.GetData(), Times.Once()); // Success
Assert.Equal(expected, actual); // Success
}

public class MyService
{
private readonly MyWebRequest _request;

public MyService(MyWebRequest request)
{
_request = request;
}

public string GetData()
{
var data = _request.GetData();
return data;
}
}

public class MyWebRequest
{
public virtual string GetData()
{
// make WebRequest
return string.Empty;
}
}

我的自动修复版本:3.6.5.0

最佳答案

MyWebRequest 是一个具体的(非抽象的)类,AutoFixture.AutoMoq 不会使用 Moq 创建那些类。这是设计的,因为 AutoFixture 内核已经处理了“普通”类的创建。

You can change this behaviour如果你愿意的话。不过,我通常会考虑必须这样做是一种设计味道。不过,在这种情况下很难判断,因为您已经很好地将问题减少到最小程度。但是,问题在于,如此处所示,MyService 不会增加任何值。我确定这不是您的真实代码的样子,但由于我无法猜测您的真实代码的样子,所以我无法判断是否可以进行更好的设计。

关于c# - AutoFixture+Moq - 卡住模拟类以防止设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42606650/

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