Как проверить эквивалентность с помощью Fluent Assertion Should (). BeEquivalentTo () при использовании производных классов - PullRequest
1 голос
/ 25 февраля 2020

У меня возникают проблемы при попытке заставить 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, похоже, игнорирует свойства, определенные в объекте, и рассматривает объект только как коллекцию.

Как получить структуру для проверки свойств и содержимого коллекция?

Редактировать Кажется, что это известная проблема

Кто-нибудь знает об обходном пути?

1 Ответ

1 голос
/ 25 февраля 2020

Это известная проблема при сравнении классов, которая реализует IEnumerable и имеет дополнительные свойства для сравнения.

Вот способ взломать сравнение.

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>());
}
...