NUnit 中的 EqualTo() 和 EquivalentTo() 有什么区别?

发布于 2024-11-18 00:58:09 字数 1579 浏览 3 评论 0 原文

当我有一个 Dictionary;实际,然后创建一个全新的Dictionary;预期与实际值相同。

  • 调用 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;
}

When I have a Dictionary<string, int> actual and then create a completely new Dictionary<string, int> expected with the same values as actual.

  • Calling Assert.That(actual, Is.EqualTo(expected)); makes the test pass.

  • When using Assert.That(actual, Is.EquivalentTo(expected)); the test doesn't pass.

What is the difference between EqualTo() and EquivalentTo()?

Edit:

The message of the exception when the test doesn't pass is as follows:

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]] >

My code looks like this:

[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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

老街孤人 2024-11-25 00:58:09

问题标题迫使我声明以下内容:

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

但是,就您而言,订购没有问题。这里的要点是 Is.EqualTo() 有用于字典比较的额外代码,如 此处

并非如此Is.EquivalentTo()。在您的示例中,它将使用 object.Equals() 比较 KeyValuePair> 类型的值是否相等。由于字典值是引用类型List,因此使用引用相等来比较它们。

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

The question title forces me to state the following:

For enumerations, Is.EquivalentTo() does the comparison allowing any order of the elements.
In contrast, Is.EqualTo() takes into account the exact order of the elements, like Enumerable.SequenceEqual() does.

However, in your case, there is no issue with ordering. The main point here is that Is.EqualTo() has extra code for dictionary comparison, as stated here.

Not so Is.EquivalentTo(). In your example, it will compare values of type KeyValuePair<string,List<int>> for equality, using object.Equals(). Since the dictionary values are of reference type List<int>, reference equality is used for comparing them.

If you modify your example such that the List {1, 2} is only instantiated once and used in both dictionaries, Is.EquivalentTo() will succeed.

过去的过去 2024-11-25 00:58:09

两者都适合我:

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)它迭代两者以找出差异。我猜它等于

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

Both works for me:

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

  • Is.EqualTo() inside NUnit, if both objects are ICollection, uses CollectionsEqual(x,y) which iterates both to find the difference. I guess it's equal to Enumerable.SequenceEqual(x,y)

  • Is.EquivalentTo does this immediate because support sequences only: EquivalentTo(IEnumerable)

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