2011-09-07 39 views
1

我使用VS 2010和运行我的单元测试与NUnit的。下面的线正确地检测,如果两个列表有所不同:如何有NUnit的收集比较调用的ToString时存在差异

 CollectionAssert.AreEqual(expected, actual); 

不过,我想比下面的一个更好的错误消息:

Expected and actual are both <System.Collections.Generic.List`1[MyNamespace.MyClass]> with 2 elements 
    Values differ at index [0] 
    Expected: <MyNamespace.MyClass> 
    But was: <MyNamespace.MyClass> 

在MyNamespace.MyClass,我已实现了以下方法:

public new string ToString() 

我希望NUnit的输出如下:

Expected and actual are both <System.Collections.Generic.List`1[MyNamespace.MyClass]> with 2 elements 
    Values differ at index [0] 
    Expected: <24 ounces of cold beer> 
    But was: <2.4 ounces of rotten tomatoes> 

然而,NUnit的不调用它。我错过了什么?

回答

5

隐藏object而不是覆盖它的方法。使用此:

public override string ToString() 

基本上NUnit的只是调用object.ToString() - 你还没有覆盖。除非它专门找了一个方法与反思,也不会发现你的一个 - 和压倒一切的是做到这一点的惯用方式。这只是一个简单的错误,还是你意味着影子出于某种原因的方法?

+0

这工作,并顺便说一句我爱你的书! –

+0

@Arne:Goodo :) –

+0

新的而不是重写只是一个错字 –