鉴于这些类:
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));
}