【问题标题】:What is difference between EqualTo() and EquivalentTo() in NUnit?NUnit 中的 EqualTo() 和 EquivalentTo() 有什么区别?
【发布时间】:2011-09-24 22:52:34
【问题描述】:

当我有一个Dictionary<string, int> actual,然后创建一个与实际值相同的全新Dictionary<string, int> expected

  • 调用Assert.That(actual, Is.EqualTo(expected)); 使测试通过。

  • 使用Assert.That(actual, Is.EquivalentTo(expected)); 时,测试未通过。

EqualTo()EquivalentTo() 有什么区别?

编辑:

测试不通过时的异常信息如下:

Zoozle.Tests.Unit.PredictionTests.ReturnsDriversSelectedMoreThanOnceAndTheirPositions:
Expected: equivalent to < [Michael Schumacher, System.Collections.Generic.List`1[System.Int32]] >
But was:  < [Michael Schumacher, System.Collections.Generic.List`1[System.Int32]] >

我的代码如下所示:

[Test]
public void ReturnsDriversSelectedMoreThanOnceAndTheirPositions()
{
    //arrange
    Prediction prediction = new Prediction();

    Dictionary<string, List<int>> expected = new Dictionary<string, List<int>>()
    {
        { "Michael Schumacher", new List<int> { 1, 2 } }
    };

    //act
    var actual = prediction.CheckForDriversSelectedMoreThanOnce();

    //assert
    //Assert.That(actual, Is.EqualTo(expected));
    Assert.That(actual, Is.EquivalentTo(expected));
}

public Dictionary<string, List<int>> CheckForDriversSelectedMoreThanOnce()
{
    Dictionary<string, List<int>> expected = new Dictionary<string, List<int>>();
    expected.Add("Michael Schumacher", new List<int> { 1, 2 });

    return expected;
}

【问题讨论】:

  • Assert.That() 抛出异常 + 给出错误的详细信息。请张贴。
  • 我不认为我关注你。 Assert.That() 抛出异常?这是新的 nunit 语法——我认为它与旧模型没有什么不同。除此之外,你想让我把这个放在哪里?
  • 他希望你在测试失败时将 NUnit 的输出添加到问题中。
  • 如果断言失败,它会抛出相应的异常。不是吗?
  • 好的,知道了 - 添加了代码和异常。

标签: .net unit-testing nunit nunit-2.5


【解决方案1】:

问题标题迫使我陈述以下内容:

对于枚举,Is.EquivalentTo() 进行比较,允许元素的任何顺序。 相比之下,Is.EqualTo() 会考虑元素的确切顺序,就像 Enumerable.SequenceEqual() 所做的那样。

但是,就您而言,订购没有问题。这里的重点是Is.EqualTo() 有额外的字典比较代码,如here 所述。

不是这样Is.EquivalentTo()。在您的示例中,它将使用object.Equals() 比较KeyValuePair&lt;string,List&lt;int&gt;&gt; 类型的值是否相等。由于字典值的引用类型为List&lt;int&gt;,因此使用引用相等来比较它们。

如果您修改示例以使 List {1, 2} 仅实例化一次并在两个字典中使用,Is.EquivalentTo() 将成功。

【讨论】:

    【解决方案2】:

    两者都适合我:

    var actual = new Dictionary<string, int> { { "1", 1 }, { "2", 2 } };
    var expected = new Dictionary<string, int> { { "1", 1 }, { "2", 2 } };
    
    Assert.That(actual, Is.EqualTo(expected)); // passed
    Assert.That(actual, Is.EquivalentTo(expected)); // passed
    

    • NUnit 内部的Is.EqualTo(),如果两个对象都是ICollection,则使用CollectionsEqual(x,y) 迭代两者以找出差异。我猜它等于Enumerable.SequenceEqual(x,y)

    • Is.EquivalentTo 立即执行此操作,因为仅支持序列:EquivalentTo(IEnumerable)

    【讨论】:

    • 你是完全正确的,我的问题是在我的字典中我有一个字符串作为键和另一个集合,一个列表,将整数作为值。正是在这一点上,EquivalentTo 抛出了一个异常。
    • @Garth:正如您已经在List&lt;int&gt; 中指出的原因。 CollectionsEqual 同时遍历两个集合进行比较,并且由于值是集合,NUnit 无法正确比较它,而只是调用 list1.Equals(list2) 执行引用比较,但失败了。
    • 这个失败是因为EquivalentTo(),对吧?因为EqualTo() 似乎工作得很好 -> 当我更改列表中的一个值时,它会很好地发现某些地方不正确。
    • @Garth:是的,我说的是EquivalentTo()。因此,将其用于仅一级集合,而不是嵌套。
    猜你喜欢
    • 2017-10-22
    • 1970-01-01
    • 2010-10-02
    • 2011-12-12
    • 2010-09-16
    • 2012-03-14
    • 2012-02-06
    • 2011-02-25
    • 2011-11-22
    相关资源
    最近更新 更多