使用 Mocha 进行 Javascript 测试中 assert.equal 和 assert.deepEqual 之间的区别? [英] The difference between assert.equal and assert.deepEqual in Javascript testing with Mocha?

查看:27
本文介绍了使用 Mocha 进行 Javascript 测试中 assert.equal 和 assert.deepEqual 之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Mocha 来测试 Express.js 应用程序中的一个小模块.在这个模块中,我的一个函数返回一个数组.我想测试数组对于给定输入是否正确.我是这样做的:

I'm using Mocha to test a small module in my Express.js application. In this module, one of my functions returns an array. I want to test whether or not the array is correct for a given input. I am doing so like this:

suite('getWords', function(){
    test("getWords should return list of numbers", function() {
        var result = ['555', '867', '5309'];
        assert.equal(result, getWords('555-867-5309'));
    });
});

运行时,我收到以下断言错误:

When this runs, I get the following assertion error:

AssertionError: ["555","867","5309"] == ["555","867","5309"]

然而,当我将我的测试更改为 assert.deepEqual 时,测试顺利通过.我想知道是否是 == vs === 的情况,但是如果我输入

However, when I change my test to an assert.deepEqual, the test passes fine. I was wondering if it was a case of == vs ===, but if I enter

[1,2,3] === [1,2,3]

进入 node.js 命令行,我仍然得到错误.

into the node.js command line, I still get false.

为什么数组不像其他值那样比较(例如 1 == 1)?assert.equal 和 assert.deepEqual 有什么区别?

Why do arrays not compare the way other values do (e.g. 1 == 1)? and what is the difference between assert.equal and assert.deepEqual?

推荐答案

为什么数组不像其他值那样比较(例如 1==1)

Why do arrays not compare the way other values do (e.g. 1==1)

数字、字符串、布尔值、nullundefined 是值,并按您的预期进行比较.1 == 1'a' == 'a',依此类推.====== 在值的情况下的区别在于 == 会先尝试进行类型转换,这就是为什么'1' == 1不是 '1' === 1.

Numbers, strings, booleans, null, and undefined are values, and are compared as you might expect. 1 == 1, 'a' == 'a', and so on. The difference between === and == in the case of values is that == will attempt to perform type conversion first, which is why '1' == 1 but not '1' === 1.

另一方面,数组是对象.===== 在这种情况下并不表示操作数在语义上相等,而是表示它们引用同一个对象.

Arrays, on the other hand, are objects. === and == in this case do not signify that the operands are semantically equal, but that they refer to the same object.

assert.equal 和 assert.deepEqual 有什么区别?

what is the difference between assert.equal and assert.deepEqual?

assert.equal 的行为如上所述.如果参数是 !=,它实际上会失败,如您所见 在来源.因此,它对您的数字字符串数组失败,因为尽管它们本质上是等效的,但它们不是同一个对象.

assert.equal behaves as explained above. It actually fails if the arguments are !=, as you can see in the source. Thus it fails for your arrays of numbers strings because although they are essentially equivalent, they are not the same object.

另一方面,深度(又名结构)相等不测试操作数是否是同一个对象,而是测试它们是否相等.从某种意义上说,您可以说它强制将对象作为值进行比较.

Deep (aka structural) equality, on the other hand, does not test whether the operands are the same object, but rather that they're equivalent. In a sense, you could say it forces objects to be compared as though they're values.

var a = [1,2,3]  
var b = a              // As a and b both refer to the same object
a == b                 // this is true
a === b                // and this is also true

a = [1,2,3]            // here a and b have equivalent contents, but do not
b = [1,2,3]            // refer to the same Array object.
a == b                 // Thus this is false.

assert.deepEqual(a, b) // However this passes, as while a and b are not the 
                       // same object, they are still arrays containing 1, 2, 3

assert.deepEqual(1, 1) // Also passes when given equal values

var X = function() {}
a = new X
b = new X
a == b                 // false, not the same object
assert.deepEqual(a, b) // pass, both are unadorned X objects
b.foo = 'bar'
assert.deepEqual(a, b) // fail!

这篇关于使用 Mocha 进行 Javascript 测试中 assert.equal 和 assert.deepEqual 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆