与摩卡的Javascript测试assert.equal和assert.deepEqual的区别? [英] The difference between assert.equal and assert.deepEqual in Javascript testing with Mocha?

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

问题描述

我用摩卡来测试我的前press.js申请一个小模块。在这个模块中,我的功能之一返回数组。我想测试数组是否是给定输入正确的。我这样做是这样的:

 套件('getWords',函数(){
    测试(getWords应该返回号码列表功能(){
        VAR的结果= ['555','867','5309'];
        assert.equal(结果,getWords('555-867-5309'));
    });
});

在此运行,我得到以下断言错误:

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

然而,当我改变我的测试,以一个 assert.deepEqual ,测试通过罚款。我想知道,如果它是 == VS === 的情况下,但如果我输入

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

到node.js的命令行,我仍然得到错误的。

为什么阵列比不上其他的方式做值(例如 1 == 1 )?和assert.equal和assert.deepEqual之间的区别是什么呢?


解决方案

  

为什么阵列比不上其他的方式做值(例如1 == 1)


数字,字符串,布尔值,未定义的值,你可能期望进行比较。 1 == 1 'A'=='一',等等。 === == 中值的情况是,之间的区别== 将首先尝试进行类型转换,这就是为什么'1'== 1 不过的'1'=== 1

阵列,在另一方面,是对象。 === == 在这种情况下不表示该操作数在语义上相等的,但是他们的指的是同一对象


  

是assert.equal和assert.deepEqual有何区别?


assert.equal 表现为如上所述。如果参数!= ,你可以看到的在源。因此它不能对您的数字字符串数组,因为尽管它们基本上是等价的,它们不是同一对象

深(又名结构)平等,另一方面,不测试操作数是否是同一个对象,而是他们是等价的。在某种意义上,你可以说这迫使对象作为虽然他们的值进行比较。

  VAR A = [1,2,3]
变种B = A //作为a和b都指代相同的对象
一个== b //这是真的
一个=== b //这也是事实一个= [1,2,3] //这里a和b的等效内容,但不
B = [1,2,3] //指相同Array对象。
一个== b //因此,这是错误的。assert.deepEqual(A,B)//然而此通过,作为同时a和b是不
                       //同一个对象,它们仍然含有1,2,3阵列assert.deepEqual(1,1)//给予同等值时,也通过变种X =函数(){}
A =新的X
B =新的X
一个== b //假的,不是同一个对象
assert.deepEqual(A,B)//传球,都是朴实点¯x对象
b.foo ='吧'
assert.deepEqual(A,B)//失败!

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

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]

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

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?

解决方案

Why do arrays not compare the way other values do (e.g. 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.

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

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!

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

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