gpt4 book ai didi

c# - FluentAssertions 排除重复配置

转载 作者:行者123 更新时间:2023-11-30 14:22:21 27 4
gpt4 key购买 nike

我有这样的单元测试代码:

// 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");

我怎样才能排除选项

我想做这样的事情:

// 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");

最佳答案

在外部声明选项委托(delegate)作为基础

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");

对于需要在基础上构建的其他断言,您必须调用委托(delegate)并附加其他选项

// 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");

关于c# - FluentAssertions 排除重复配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50166216/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com