如何对集合中的属性使用FluentAssertions中的排除? [英] How to use Exclude in FluentAssertions for property in collection?

查看:80
本文介绍了如何对集合中的属性使用FluentAssertions中的排除?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两节课:

public class ClassA
{
  public int? ID {get; set;}
  public IEnumerable<ClassB> Children {get; set;}
}

public class ClassB
{
  public int? ID {get; set;}
  public string Name {get; set;}
}

我想使用流畅的断言与ClassA实例进行比较.但是我想忽略这些ID(因为ID将在保存后分配).

I want to use fluent assertions to compare to ClassA instances. However I want to ignore the IDs (because the IDs will have been assigned after the save).

我知道我可以做到:

expectedA.ShouldBeEquivalentTo(actualA, options => options.Excluding(x => x.PropertyPath == "Children[0].ID"));

显然,我可以对集合中的每个ClassB重复该操作.但是,我正在寻找一种排除所有ID的方法(而不是对每个元素进行排除).

Which I can obviously repeat for each ClassB in the collection. However I'm looking for a way to exclude the all the IDs (rather than doing an exclude for each element).

我已阅读

I've read this question however if I remove the [0] indexers the assertions fail.

这可能吗?

推荐答案

那又如何?

expected.ShouldBeEquivalentTo(actualA, options => options.Excluding(su => 
   (su.RuntimeType == typeof(ClassB)) && (su.PropertyPath.EndsWith("Id")));`

或者您可以在属性路径上进行RegEx匹配,例如

Or you could do a RegEx match on the property path, such as

expected.ShouldBeEquivalentTo(actualA, options => options.Excluding(su => (Regex.IsMatch
   ("Children\[.+\]\.ID"));

我实际上很喜欢最后一个,但是正则表达式的东西使阅读起来有些困难.也许我应该使用一种方法来扩展ISubjectInfo,以将路径与通配符模式进行匹配,以便您可以执行以下操作:

I actually like that last one, but the regex stuff makes it a bit difficult to read. Maybe I should extend ISubjectInfo with a method to match the path against a wildcard pattern, so that you can do this:

expected.ShouldBeEquivalentTo(actualA, options => options
  .Excluding(su => su.PathMatches("Children[*].ID")));

这篇关于如何对集合中的属性使用FluentAssertions中的排除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆