gpt4 book ai didi

autofixture - 为什么 Autofixture Likeness 的行为不像我对这两个测试之一的预期那样?

转载 作者:行者123 更新时间:2023-12-02 00:10:43 34 4
gpt4 key购买 nike

给定这些类:

public class DrumAndBassBand
{
public Drums Drum { get; set; }
public Bass Bass { get; set; }
}

public class Instrument
{
public string Name { get; set; }
public int SerialNumber { get; set; }
}
public class Drums : Instrument { }
public class Bass : Instrument { }

为什么这个测试通过...

[Fact]
public void DrumAndBassBand_Equality_Behaves_As_Expected_Version_One()
{
// arrange
var template = new Fixture().Create<DrumAndBassBand>();

// act
var createdBand = new DrumAndBassBand {Drum = template.Drum, Bass = template.Bass};

// assert
var createdLikeness = createdBand.AsSource().OfLikeness<DrumAndBassBand>()
.Without(x => x.Bass)
.CreateProxy();
createdLikeness.Drum = createdBand.Drum;

Assert.True(createdLikeness.Equals(template));

var templateLikeness = template.AsSource().OfLikeness<DrumAndBassBand>()
.Without(x => x.Bass)
.CreateProxy();
templateLikeness.Drum = template.Drum;

Assert.True(templateLikeness.Equals(createdBand));
}

...这个失败了? (区别在于 DrumAndBaseBand 实例化)

[Fact]
public void DrumAndBassBand_Equality_Behaves_As_Expected_Version_Two()
{
// arrange
var template = new Fixture().Create<DrumAndBassBand>();

// act
var createdBand =
new DrumAndBassBand
{
Drum = new Drums { Name = template.Drum.Name, SerialNumber = template.Drum.SerialNumber },
Bass = new Bass { Name = template.Bass.Name, SerialNumber = template.Bass.SerialNumber }
};

// assert
var createdLikeness = createdBand.AsSource().OfLikeness<DrumAndBassBand>()
.Without(x => x.Bass)
.CreateProxy();
createdLikeness.Drum = createdBand.Drum;

Assert.True(createdLikeness.Equals(template));

var templateLikeness = template.AsSource().OfLikeness<DrumAndBassBand>()
.Without(x => x.Bass)
.CreateProxy();
templateLikeness.Drum = template.Drum;

Assert.True(templateLikeness.Equals(createdBand));
}

最佳答案

在第二个测试中,DrumBass 实例与您尝试比较的模板不同。

您始终可以运行Likeness(无需创建代理)并检查输出:

Test 'DrumAndBassBand_Equality_Behaves_As_Expected_Version_Two' failed: 

Ploeh.SemanticComparison.LikenessException:
The provided value DrumAndBassBand did not match the expected value DrumAndBassBand.

The following members did not match:
- Drum.

这基本上意味着您必须在创建用于比较 Drum 实例的 Likeness 时提供提示。

测试的前半部分变成:

var createdLikeness = createdBand
.AsSource().OfLikeness<DrumAndBassBand>()
.With(x => x.Drum)
.EqualsWhen((s, d) => s.Drum == createdBand.Drum)
.Without(x => x.Bass)
.CreateProxy();

源目的地的 Drums 等于 other Drums 当它真的是一个 createdBand 实例 Drum.

同样,测试的后半部分变为:

var templateLikeness = template
.AsSource().OfLikeness<DrumAndBassBand>()
.With(x => x.Drum)
.EqualsWhen((s, d) => s.Drum == template.Drum)
.Without(x => x.Bass)
.CreateProxy();

以上允许您进行非常灵活的比较(并且您始终可以进一步自定义它)。

关于autofixture - 为什么 Autofixture Likeness 的行为不像我对这两个测试之一的预期那样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15605241/

34 4 0
文章推荐: wpf - 是否可以在 MVVM Light 消息中发送 List