我有两个类:
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将在保存后分配)。
我知道我能做到:
expectedA.ShouldBeEquivalentTo(actualA, options => options.Excluding(x => x.PropertyPath == "Children[0].ID"));
显然,我可以对集合中的每个ClassB重复此操作。然而,我正在寻找一种方法来排除所有的ID(而不是对每个元素进行排除)。
我读过this question,但是如果我删除索引器,断言就会失败。
这个是可能的吗?
发布于 2014-03-05 04:25:58
关于?
expected.ShouldBeEquivalentTo(actualA, options => options.Excluding(su =>
(su.RuntimeType == typeof(ClassB)) && (su.PropertyPath.EndsWith("Id")));`
或者您可以在属性路径上执行RegEx匹配,例如
expected.ShouldBeEquivalentTo(actualA, options => options.Excluding(su => (Regex.IsMatch
("Children\[.+\]\.ID"));
我实际上很喜欢最后一个,但是正则表达式的东西让它有点难以阅读。也许我应该用一个方法来扩展ISubjectInfo
,以便根据通配符模式匹配路径,这样您就可以这样做:
expected.ShouldBeEquivalentTo(actualA, options => options
.Excluding(su => su.PathMatches("Children[*].ID")));
发布于 2015-08-13 02:16:42
我刚刚遇到了一个类似的问题,最新版本的FluentAssertions改变了一些事情。
My objects包含其他对象的字典。字典中的对象包含我想要排除的其他对象。我的场景是关于测试Json序列化的,我忽略了某些属性。
这对我来说很有效:
gotA.ShouldBeEquivalentTo(expectedB , config =>
config
.Excluding(ctx => ctx.SelectedMemberInfo.MemberType == typeof(Venue))
.Excluding(ctx => ctx.SelectedMemberInfo.MemberType == typeof(Exhibit))
.Excluding(ctx => ctx.SelectedMemberInfo.MemberType == typeof(Content))
.Excluding(ctx => ctx.SelectedMemberInfo.MemberType == typeof(Survey))
.Excluding(ctx => ctx.SelectedMemberInfo.MemberType == typeof(Media))
);
我花了一些时间才弄明白怎么做,但它真的很有用!
发布于 2014-03-03 17:41:08
简单的方法是直接在集合上设置断言,并将其排除在ClassA
等价断言上:
expectedA.ShouldBeEquivalentTo(expectedB,
o => o.Excluding(s => s.PropertyInfo.Name == "Children"));
expectedA.Children.ShouldBeEquivalentTo(expectedB.Children,
o => o.Excluding(s => s.PropertyInfo.Name = "Id"));
https://stackoverflow.com/questions/22142576
复制相似问题