在earlier question我询问了Autofixture的CreateProxy method,potential bug被识别出来。使用Autofixture的CreateProxy来使用Likeness,SemanticComparison功能时遇到问题
我不认为这个失败的测试是因为这个,而是我对Likeness.Without(...)。CreateProxy()语法是如何工作的继续混淆。考虑下面失败的测试中,我通过创建对象的新实例,考虑到它的创作使original test非常轻微更加复杂是SUT:
[Fact]
public void Equality_Behaves_As_Expected()
{
// arrange: intent -> use the fixture-created Band as Object Mother
var template = new Fixture().Create<Band>();
// act: intent -> instantiated Band *is* the SUT
var createdBand = new Band {Brass = template.Brass,
Strings = template.Brass};
// intent -> specify that .Brass should not be considered in comparison
var likeness = template.AsSource().OfLikeness<Band>().
Without(x => x.Brass).CreateProxy(); // Ignore .Brass property
// per [https://stackoverflow.com/a/15476108/533958] explicity assign
// properties to likeness
likeness.Strings = template.Strings;
likeness.Brass = "foo"; // should be ignored
// assert: intent -> check equality between created Band & template Band
// to include all members not excluded in likeness definition
likeness.Should().Be(createdBand); // Fails
likeness.ShouldBeEquivalentTo(createdBand); // Fails
Assert.True(likeness.Equals(createdBand)); // Fails
}
这里的乐队:
public class Band
{
public string Strings { get; set; }
public string Brass { get; set; }
}
我的earlier question不够复杂,无法帮助我理解Likeness
的一般Source
。
源应该是SUT的输出,在这种情况下,它将与由AutoFixture创建的模板实例进行比较?
或者应该来源为模板由AutoFixture创建的实例,在这种情况下,它将与SUT的输出进行比较?
编辑:修正了一个错误的测试
我意识到,我已经分配了错误的template.Brass
属性都的Brass
和新Band
实例的Strings
财产。更新后的测试反映了var createdBand = new Band {Brass = template.Brass, Strings = template.Strings}
的更正,现在所有六个断言都通过了。
[Fact]
public void Equality_Behaves_As_Expected()
{
// arrange: intent -> use the fixture-created Band as Object Mother
var template = new Fixture().Create<Band>();
// act: intent -> instantiated Band *is* the SUT
var createdBand = new Band {Brass = template.Brass, Strings = template.Strings};
// likeness of created
var createdLikeness = createdBand.AsSource().OfLikeness<Band>().
Without(x => x.Brass).CreateProxy(); // .Brass should not be considered in comparison
// https://stackoverflow.com/a/15476108/533958 (explicity assign properties to likeness)
createdLikeness.Strings = createdBand.Strings;
createdLikeness.Brass = "foo"; // should be ignored
// likeness of template
var templateLikeness = template.AsSource().OfLikeness<Band>()
.Without(x => x.Brass)
.CreateProxy();
templateLikeness.Strings = template.Strings;
templateLikeness.Brass = "foo";
// assert: intent -> compare created Band to template Band
createdLikeness.Should().Be(template);
createdLikeness.ShouldBeEquivalentTo(template);
Assert.True(createdLikeness.Equals(template));
templateLikeness.Should().Be(createdBand);
templateLikeness.ShouldBeEquivalentTo(createdBand);
Assert.True(templateLikeness.Equals(createdBand));
}
关于明确指定属性,[这是怎么回事下一个版本还有待提高] (http://stackoverflow.com/questions/15470997/why-doesnt-simple-test-pass-using-autofixture-freeze-semanticcomparison-likene#comment22060162_15476108)。 – 2013-03-22 07:24:23
@NikosBaxevanis谢谢你的工作!在此之前,我已经完成了你对我之前的问题的回答中所建议的内容,即在将要检查平等的实例中明确指定属性值,除了排除使用.Without(...)语法的相似性。我有这个部分吗? – Jeff 2013-03-22 17:04:31
@Lumiris是的,这是正确的。 – 2013-03-22 19:52:21