Ignore internal properties in ShouldBeEquivalentTo

佐手、 提交于 2019-12-23 19:11:17

问题


Is there a way to ignore internal properties of a class when doing ShouldBeEquivalentTo?

For example, in the class below I want to exclude the MetaData property from the object graph comparison.

public class SomeObject 
{
    Public string SomeString { get; set; }
    internal MetaData MetaData { get; set; }
}

I would prefer to not use

someObject.ShouldBeEquivalentTo(someOtherObject, options =>     
    options.Excluding(info => info.SelectedMemberPath == "MetaData")

Because I might have more than 1 internal property and setting up this for all those properties would be tedious.


回答1:


There is the IMemberSelectionRule interface in the FluentAssertions library:

Represents a rule that defines which members of the subject-under-test to include while comparing two objects for structural equality.

Implementing this interface allows to exclude all the internal properties at once (where IsAssembly property is 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));
    }
  }

Now the rule can be utilized in unit tests:

  someObject.ShouldBeEquivalentTo(someOtherObject, options => options.Using(
    new AllExceptNonPublicPropertiesSelectionRule()));


来源:https://stackoverflow.com/questions/41767863/ignore-internal-properties-in-shouldbeequivalentto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!