Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 当存在差异时,如何让NUnit集合比较调用ToString_C#_Unit Testing_Nunit - Fatal编程技术网

C# 当存在差异时,如何让NUnit集合比较调用ToString

C# 当存在差异时,如何让NUnit集合比较调用ToString,c#,unit-testing,nunit,C#,Unit Testing,Nunit,我正在使用VS2010,并使用NUnit运行单元测试。下一行正确检测两个列表是否不同: CollectionAssert.AreEqual(expected, actual); 但是,我希望得到比以下更好的错误消息: Expected and actual are both <System.Collections.Generic.List`1[MyNamespace.MyClass]> with 2 elements Values differ at index

我正在使用VS2010,并使用NUnit运行单元测试。下一行正确检测两个列表是否不同:

        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>
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能够输出以下内容:

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>
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>
Expected和actual都有2个元素
值在索引[0]处不同
预期:
但是:
但是,NUnit不会调用它。我遗漏了什么?

您对
对象隐藏了该方法,而不是覆盖它。使用以下命令:

public override string ToString()
基本上,NUnit只是调用
object.ToString()
——您没有覆盖它。除非它专门寻找一个带有反射的新方法,否则它不会找到您的方法——重写是实现这一点的惯用方法。这仅仅是一个简单的错误,还是您出于某种原因想要隐藏该方法?

您对
对象隐藏了该方法,而不是覆盖它。使用以下命令:

public override string ToString()

基本上,NUnit只是调用
object.ToString()
——您没有覆盖它。除非它专门寻找一个带有反射的新方法,否则它不会找到您的方法——重写是实现这一点的惯用方法。这仅仅是一个简单的错误,还是出于某种原因,您想隐藏该方法?

new代替override只是一个输入错误new代替override只是一个输入错误