在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