忽略 ShouldBeEquivalentTo 中的内部属性
Ignore internal properties in ShouldBeEquivalentTo
有没有办法在执行 ShouldBeEquivalentTo 时忽略 class 的内部属性?
例如,在下面的 class 中,我想从对象图比较中排除元数据 属性。
public class SomeObject
{
Public string SomeString { get; set; }
internal MetaData MetaData { get; set; }
}
我不想使用
someObject.ShouldBeEquivalentTo(someOtherObject, options =>
options.Excluding(info => info.SelectedMemberPath == "MetaData")
因为我可能有超过 1 个内部 属性 并且为所有这些属性设置它会很乏味。
FluentAssertions库中有IMemberSelectionRule
接口:
Represents a rule that defines which members of the subject-under-test to include while comparing two objects for structural equality.
实现此接口允许一次排除所有 内部 属性(其中 IsAssembly
属性 是 true
):
internal class AllExceptNonPublicPropertiesSelectionRule : IMemberSelectionRule
{
public bool IncludesMembers
{
get { return false; }
}
public IEnumerable<SelectedMemberInfo> SelectMembers(
IEnumerable<SelectedMemberInfo> selectedMembers,
ISubjectInfo context,
IEquivalencyAssertionOptions config)
{
return selectedMembers.Except(
config.GetSubjectType(context)
.GetNonPrivateProperties()
.Where(p => p.GetMethod.IsAssembly)
.Select(SelectedMemberInfo.Create));
}
}
现在可以在单元测试中使用该规则:
someObject.ShouldBeEquivalentTo(someOtherObject, options => options.Using(
new AllExceptNonPublicPropertiesSelectionRule()));
有没有办法在执行 ShouldBeEquivalentTo 时忽略 class 的内部属性?
例如,在下面的 class 中,我想从对象图比较中排除元数据 属性。
public class SomeObject
{
Public string SomeString { get; set; }
internal MetaData MetaData { get; set; }
}
我不想使用
someObject.ShouldBeEquivalentTo(someOtherObject, options =>
options.Excluding(info => info.SelectedMemberPath == "MetaData")
因为我可能有超过 1 个内部 属性 并且为所有这些属性设置它会很乏味。
FluentAssertions库中有IMemberSelectionRule
接口:
Represents a rule that defines which members of the subject-under-test to include while comparing two objects for structural equality.
实现此接口允许一次排除所有 内部 属性(其中 IsAssembly
属性 是 true
):
internal class AllExceptNonPublicPropertiesSelectionRule : IMemberSelectionRule
{
public bool IncludesMembers
{
get { return false; }
}
public IEnumerable<SelectedMemberInfo> SelectMembers(
IEnumerable<SelectedMemberInfo> selectedMembers,
ISubjectInfo context,
IEquivalencyAssertionOptions config)
{
return selectedMembers.Except(
config.GetSubjectType(context)
.GetNonPrivateProperties()
.Where(p => p.GetMethod.IsAssembly)
.Select(SelectedMemberInfo.Create));
}
}
现在可以在单元测试中使用该规则:
someObject.ShouldBeEquivalentTo(someOtherObject, options => options.Using(
new AllExceptNonPublicPropertiesSelectionRule()));