适当的断言消息

时间:2013-06-20 13:22:04

标签: c# visual-studio testing nunit assert

在测试中,我试图断言两个TextSpan列表的相等/等价,如下所示:

var expectedSpans = new List<TextSpan>()
    {
     new TextSpan { iStartLine = 1, iEndLine = 1, iStartIndex = 1, iEndIndex = 1}
    };

var obtainedSpans = new List<TextSpan>()
    {
     new TextSpan { iStartLine = 2, iEndLine = 2, iStartIndex = 1, iEndIndex = 1}
    };
Assert.That(obtainedSpans, Is.EqualTo(expectedSpans), "Unexpected spans found");

我得到的信息是:

Expected tags were not obtained.
  Expected is <System.Collections.Generic.List`1[Microsoft.VisualStudio.TextManager.Interop.TextSpan]> with 1 elements, actual is <System.Collections.Generic.List`1[Microsoft.VisualStudio.TextManager.Interop.TextSpan]> with 1 elements
  Values differ at index [0]
  Expected: Microsoft.VisualStudio.TextManager.Interop.TextSpan
  But was:  Microsoft.VisualStudio.TextManager.Interop.TextSpan

如何获得更详细的信息,至少显示所有值,以确定相等/等价丢失的位置? 在等价断言的情况下,该消息也没有提供信息。

1 个答案:

答案 0 :(得分:1)

您应该使用CollectionAssert.AreEqual(expectedSpans, obtainedSpan, "Unexpected spans found")来进行正确的列表相等性断言。

顺便说一句,两句话: - 使用Assert.AreEquals()代替Assert.That(..., IsEqualTo()) - 在获得断言失败的可读性之前,始终将预期置于预期之前。