我有这样的单元测试代码:
// version3 should be a copy of version2 with some differences
version3.Data.ShouldBeEquivalentTo(version2,
option => option
.Excluding(x => x.Id)
.Excluding(x => x.Status)
.Excluding(x => x.Stale)
.Excluding(x => x.Body)
.Excluding(x => x.CreatedBy)
.Excluding(x => x.UpdatedBy),
"because version3 is version2 updated");
然后再次
// version4 should be a copy of version3 with some differences
version4.Data.ShouldBeEquivalentTo(version3,
option => option
.Excluding(x => x.Id)
.Excluding(x => x.Status)
.Excluding(x => x.Stale)
.Excluding(x => x.Body)
.Excluding(x => x.CreatedBy)
.Excluding(x => x.UpdatedBy),
"because version4 is version3 updated");
如何计算option
?
我想做这样的事情:
// version3 should be a copy of version2 with some differences
version3.Data.ShouldBeEquivalentTo(version2,
option => baseOption,
"because version3 is version2 updated");
甚至可能是这样:
// version3 should be a copy of version2 with some differences
version3.Data.ShouldBeEquivalentTo(version2,
option => baseOption
.Excluding(x => x.OtherProperty),
"because version3 is version2 updated");
答案 0 :(得分:4)
在外部声明选项委托作为基础
Func<FluentAssertions.Equivalency.EquivalencyAssertionOptions<MyDataType>,
FluentAssertions.Equivalency.EquivalencyAssertionOptions<MyDataType>>
baseOption = option => option
.Excluding(x => x.Id)
.Excluding(x => x.Status);
并将其与断言
一起使用// version3 should be a copy of version2 with some differences
version3.Data.ShouldBeEquivalentTo(version2, baseOption,
"because version3 is version2 updated");
对于需要在基础上构建的其他断言,您必须调用委托并附加其他选项
// version3 should be a copy of version2 with some differences
version3.Data.ShouldBeEquivalentTo(version2,
option => baseOption(option)
.Excluding(x => x.OtherProperty),
"because version3 is version2 updated");
您应该注意,在较新版本的框架中已弃用当前使用的语法。
version3.Should().BeEquivalentTo(version2, baseOption,
"because version3 is version2 updated");
答案 1 :(得分:2)
或者切换到v5,这样您就可以将您的主题与匿名类型进行比较,您只需要为该特定测试定义您关注的属性。