NUnit,CollectionAssert.AreEquivalent(...,...),C# 问题
我是 NUnit 新手,正在寻找有关此测试失败原因的解释?
运行测试时出现以下异常。
NUnit.Framework.AssertionException:预期:相当于
<....ExampleClass>、<....ExampleClass> > 但是是:< <....ExampleClass>、<....ExampleClass> >
using NUnit.Framework;
using System.Collections.ObjectModel;
public class ExampleClass
{
public ExampleClass()
{
Price = 0m;
}
public string Description { get; set; }
public string SKU { get; set; }
public decimal Price { get; set; }
public int Qty { get; set; }
}
[TestFixture]
public class ExampleClassTests
{
[Test]
public void ExampleTest()
{
var collection1 = new Collection<ExampleClass>
{
new ExampleClass
{Qty = 1, SKU = "971114FT031M"},
new ExampleClass
{Qty = 1, SKU = "971114FT249LV"}
};
var collection2 = new Collection<ExampleClass>
{
new ExampleClass
{Qty = 1, SKU = "971114FT031M"},
new ExampleClass
{Qty = 1, SKU = "971114FT249LV"}
};
CollectionAssert.AreEquivalent(collection1, collection2);
}
}
I'm new to NUnit and looking for an explination as to why this test fails?
I get the following exception when running the test.
NUnit.Framework.AssertionException: Expected: equivalent to < <....ExampleClass>, <....ExampleClass> >
But was: < <....ExampleClass>, <....ExampleClass> >
using NUnit.Framework;
using System.Collections.ObjectModel;
public class ExampleClass
{
public ExampleClass()
{
Price = 0m;
}
public string Description { get; set; }
public string SKU { get; set; }
public decimal Price { get; set; }
public int Qty { get; set; }
}
[TestFixture]
public class ExampleClassTests
{
[Test]
public void ExampleTest()
{
var collection1 = new Collection<ExampleClass>
{
new ExampleClass
{Qty = 1, SKU = "971114FT031M"},
new ExampleClass
{Qty = 1, SKU = "971114FT249LV"}
};
var collection2 = new Collection<ExampleClass>
{
new ExampleClass
{Qty = 1, SKU = "971114FT031M"},
new ExampleClass
{Qty = 1, SKU = "971114FT249LV"}
};
CollectionAssert.AreEquivalent(collection1, collection2);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了确定 2 个集合是否相等,NUnit 最终必须比较集合内的值。在本例中,值的类型为
ExampleClass
,它是一个class
。它没有实现任何相等性测试(例如重写 Equals 和 GetHashCode),因此 NUnit 最终将进行引用比较。这将失败,因为每个集合都包含Example
的不同实例,即使字段具有相同的值。您可以通过将以下内容添加到
ExampleClass
来修复此问题。注意:我为
GetHashCode
选择值 1,因为这是一个可变类型,并且是 GetHashCode 上唯一真正安全的返回值可变类型是常量。如果您打算将其用作Dictionary
中的键,那么您可能需要重新访问它。In order to determine if 2 collections are equal NUnit must eventually compare the values within the collection. In this case the values are of type
ExampleClass
which is aclass
. It does not implement any equality testing (such as overriding Equals and GetHashCode) so NUnit will eventually do a reference comparison. This will fail as each collection contains different instances ofExample
even though the fields have the same values.You could fix this by adding the following to the
ExampleClass
Note: I chose the value 1 for
GetHashCode
because this is a mutable type and the only truly safe return value for GetHashCode on a mutable type is a constant. If you intend to use this as a key in aDictionary<TKey,TValue>
though you will want to revisit this.您需要在
ExampleClass
上实现Equals
和GetHashCode
。如果没有这个,NUnit 就会进行引用相等性检查(“这些是完全相同的对象吗?”),而不是值相等性检查(“这些对象看起来相似吗?”)。You need to implement
Equals
andGetHashCode
on yourExampleClass
. Without that, NUnit is doing a reference equality check ("are these the exact same object?"), not a value equality one ("do these objects look alike?").