1

在测试中,我试图断言两个 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

我怎样才能获得更详细的消息,至少显示所有值让我找出相等/等价在哪里丢失?在等价断言的情况下,该消息也没有提供信息。

4

1 回答 1

1

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

顺便说一句,有两个评论: - 使用Assert.AreEquals()而不是Assert.That(..., IsEqualTo()) - 总是在获得断言失败的可读性之前放置预期。

于 2013-06-20T13:32:57.387 回答