【发布时间】:2013-03-11 15:11:00
【问题描述】:
在earlier question 中,我询问了Autofixture 的CreateProxy method,发现potential bug。
我不认为这个失败的测试是因为这个,而是我对 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));
}
【问题讨论】:
-
@NikosBaxevanis 感谢您为此所做的工作!在那之前,我已经完成了您在对我之前的问题的回答中提出的建议,即明确地将属性值分配给我将要检查相等性的实例中我期望的值,但排除的任何属性除外使用 .Without(...) 语法的相似性。我的那部分对吗?
-
@Lumiris 是的,没错。
-
从 3.0.4 及更高版本开始,值会自动复制到代理实例(这意味着,
createdLikeness.Strings = createdBand.Strings;将自动发生)
标签: unit-testing autofixture stub-data-generation semantic-comparison