我正在编写单元测试,其中SUT(被测系统)可以进行各种不同的配置。我决定(或尝试)采取另一种更清洁的方法,而不是为每种组合添加参数。对于那些不了解的人来说,Fixture
对象来自一个名为AutoFixture的库,它可以处理播种随机测试值。
这是我失败的测试 -
[Test]
public void Test()
{
var locate = Build(x => x.With(xx => xx.TicketNo, 123));
Assert.AreEqual(123, locate.TicketNo);
}
private Locate Build(Action<ICustomizationComposer<Locate>> customizationAction)
{
var fixture = new Fixture();
var customizationComposer = fixture.Build<Locate>();
customizationAction(customizationComposer);
var postProcessComposer = customizationComposer
.Without(x => x.Attachments)
.Without(x => x.Comments)
.Without(x => x.Reviews)
.Without(x => x.ScheduledCrew)
.Without(x => x.PendingDecision)
.Without(x => x.FinalDecision)
.Without(x => x.ConflictResolution);
return postProcessComposer.Create();
}
尽可能地,我尝试使用Build
方法通过Locate
方法调用创建一组带有一组忽略规则的Without
对象。这是我每次测试所需的基线对象。每个测试都可以通过With
方法调用忽略其他字段或硬代码字段值。
我希望通过,因为我设置了Build
lambda参数来将TicketNo
属性设置为123.相反,测试失败,因为AutoFixture将TicketNo
种子随机整数。< / p>
我怎样才能做到这一点?
答案 0 :(得分:0)
的解决方案!
private Locate Build(Func<IPostprocessComposer<Locate>, IPostprocessComposer<Locate>> action)
{
var customizationComposer = _fixture.Build<Locate>();
var postProcessComposer = customizationComposer
.With(x => x.TicketNo, DateTime.Now.ToFileTime())
.With(x => x.On1CallNotified, false)
.Without(x => x.Attachments)
.Without(x => x.Comments)
.Without(x => x.Reviews)
.Without(x => x.ScheduledCrew)
.Without(x => x.PendingDecision)
.Without(x => x.FinalDecision)
.Without(x => x.ConflictResolution);
postProcessComposer = action(postProcessComposer);
return postProcessComposer.Create();
}