当前位置 : 主页 > 编程语言 > c语言 >

c# – FluentAssertions分解重复配置

来源:互联网 收集:自由互联 发布时间:2021-06-25
我有像这样的单元测试代码: // version3 should be a copy of version2 with some differencesversion3.Data.ShouldBeEquivalentTo(version2, option = option .Excluding(x = x.Id) .Excluding(x = x.Status) .Excluding(x = x.Stale) .Exclud
我有像这样的单元测试代码:

// 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");
在外部声明选项委托作为基础

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");
网友评论