Assert IEnumerables

前端 未结 4 1639
死守一世寂寞
死守一世寂寞 2021-02-18 19:10

As unit testing is not used in our firm, I\'m teaching myself to unit test my own code. I\'m using the standard .net test framework for some really basic unit testing.

A

4条回答
  •  没有蜡笔的小新
    2021-02-18 19:57

    There is a simple but not obvious way to do this with NUnit 3+ 'constraint' assertions:

    Assert.That(result, Is.EqualTo(expected).AsCollection, "My failure message");
    

    This will give you the same as the linq mentioned in other answers:

    Assert.That(result.SequenceEqual(expected), "My failure message");
    

    But the AsCollection version will tell you where the first difference is, and if the lengths are different.

    See https://docs.nunit.org/articles/nunit/writing-tests/constraints/EqualConstraint.html#comparing-arrays-collections-and-ienumerables

提交回复
热议问题