NUnit,CollectionAssert.AreEquivalent(...,...),C# 问题

发布于 2024-08-25 10:31:53 字数 1349 浏览 2 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡笑忘祈一世凡恋 2024-09-01 10:31:53

为了确定 2 个集合是否相等,NUnit 最终必须比较集合内的值。在本例中,值的类型为 ExampleClass,它是一个 class。它没有实现任何相等性测试(例如重写 Equals 和 GetHashCode),因此 NUnit 最终将进行引用比较。这将失败,因为每个集合都包含 Example 的不同实例,即使字段具有相同的值。

您可以通过将以下内容添加到 ExampleClass 来修复此问题。

public override bool Equals(object o) {
  var other = o as ExampleClass;
  if ( other == null ) { return false; }
  return this.Description == other.Description
    && this.SKU == other.SKU
    && this.Price == other.Price
    && this.Qty == other.Qty;
}

public override int GetHashCode() { return 1; }

注意:我为 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 a class. 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 of Example even though the fields have the same values.

You could fix this by adding the following to the ExampleClass

public override bool Equals(object o) {
  var other = o as ExampleClass;
  if ( other == null ) { return false; }
  return this.Description == other.Description
    && this.SKU == other.SKU
    && this.Price == other.Price
    && this.Qty == other.Qty;
}

public override int GetHashCode() { return 1; }

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 a Dictionary<TKey,TValue> though you will want to revisit this.

℉服软 2024-09-01 10:31:53

您需要在 ExampleClass 上实现 EqualsGetHashCode。如果没有这个,NUnit 就会进行引用相等性检查(“这些是完全相同的对象吗?”),而不是值相等性检查(“这些对象看起来相似吗?”)。

You need to implement Equals and GetHashCode on your ExampleClass. 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?").

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文