【问题标题】:How to check equivalence using Fluent Assertion Should().BeEquivalentTo() when using derived classes使用派生类时如何使用 Fluent Assertion Should().BeEquivalentTo() 检查等价性
【发布时间】:2020-06-09 06:02:19
【问题描述】:

我在尝试让 Should().BeEquivalentTo() 处理从基类派生的类型并实现集合接口时遇到问题:

public class Entity
{
    public string Id {get; set;}
    public string Name {get; set;}
}

public class Derived : Entity, ICollection<Entity>
{
    private List<Entity> m_Children = new List<Entity>();

    public string Description { get; set; }

    public int Count => ((ICollection<Entity>)m_Children).Count;

    public bool IsReadOnly => ((ICollection<Entity>)m_Children).IsReadOnly;

    public void Add(Entity item)
    {
        ((ICollection<Entity>)m_Children).Add(item);
    }

    public void Clear()
    {
        ((ICollection<Entity>)m_Children).Clear();
    }

    public bool Contains(Entity item)
    {
        return ((ICollection<Entity>)m_Children).Contains(item);
    }

    public void CopyTo(Entity[] array, int arrayIndex)
    {
        ((ICollection<Entity>)m_Children).CopyTo(array, arrayIndex);
    }

    public IEnumerator<Entity> GetEnumerator()
    {
        return ((ICollection<Entity>)m_Children).GetEnumerator();
    }

    public bool Remove(Entity item)
    {
        return ((ICollection<Entity>)m_Children).Remove(item);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return ((ICollection<Entity>)m_Children).GetEnumerator();
    }
}

测试

[TestMethod]
public void EquivalenceTest()
{
    var expected = new Derived
    {
        Id = "123",
        Name = "abc",
        Description = "def"
    };
    var actual = new Derived
    {
        Id = "121",
        Name = "xyz",
        Description = "def"
    };    

    actual.Should().BeEquivalentTo(expected);   // This succeeds, but should fail
}

对 BeEquivalentTo 的调用似乎忽略了对象中定义的属性,只将对象视为集合。

如何让框架检查集合的属性和内容?

编辑 好像是a known issue

有人知道解决方法吗?

【问题讨论】:

  • actualChild 定义在哪里?
  • 道歉 - 剪切和粘贴错误。我现在已经编辑了原始帖子。
  • 还是有问题 - Derived 在声明中有错字(冒号但没有基本类型)。
  • 这似乎是一个已知问题 - 我添加了一个链接。但是有人知道解决方法吗?

标签: c# fluent-assertions


【解决方案1】:

比较实现 IEnumerable 并有额外属性要比较的类时,它是 known issue

这是一种破解比较的方法。

public class Entity : IEnumerable<int>
{
    private int[] ints = new[] { 1 };

    public int Id { get; set; }

    public string Name { get; set; }

    public IEnumerator<int> GetEnumerator() => ((IEnumerable<int>)ints).GetEnumerator();

    IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable<int>)ints).GetEnumerator();
}

[TestMethod]
public void EquivalenceTest()
{
    var expected = new Entity
    {
        Id = 1,
        Name = "abc",
    };

    var actual = new Entity
    {
        Id = 1,
        Name = "abc",
    };

    actual.Should().BeEquivalentTo(expected, opt => opt
        .Using<Entity>(e =>
            e.Subject.Should().Match<Entity>(f => f.Name == e.Expectation.Name)
            .And.Subject.Should().Match<Entity>(f => f.Id == e.Expectation.Id)
                .And.Subject.Should().BeEquivalentTo(e.Expectation)
        )
        .WhenTypeIs<Entity>());
}

【讨论】:

  • 有什么方法可以更通用地实现这个“hack”,这样它就可以检查类的公共属性,而不必手动编码每个属性?
  • 您似乎不喜欢比较每个属性。只需使用 actual.Should().BeEquivalentTo(expected, opt => opt .Using(e => e.Subject.Should().BeEquivalentTo(e.Expectation)) .WhenTypeIs());工作。
猜你喜欢
  • 2019-05-01
  • 2019-09-30
  • 1970-01-01
  • 2018-08-09
  • 2018-08-25
  • 2014-11-13
  • 2013-04-15
  • 2022-04-19
  • 1970-01-01
相关资源
最近更新 更多