Javascript 使用mocha&;将数组对象与字符串进行比较&;柴。

Javascript 使用mocha&;将数组对象与字符串进行比较&;柴。,javascript,testing,mocha.js,chai,Javascript,Testing,Mocha.js,Chai,我第一次使用摩卡和柴,不知道发生了什么?我想说我的洗牌方法已经移动了数组对象,第一个数组对象不再是“sam1”IE- 这就是错误所在- AssertionError: expected [ Array(9) ] to equal true 我如何将两者进行比较以使其成为现实?或者有没有更好的方法来显示数组已被洗牌?类似的内容? assert.notEqual(组[0],“sam1”); 您可以在这里找到可用函数的列表 assert.equal()的第一个参数是比较的位置, 所以 应该是

我第一次使用摩卡和柴,不知道发生了什么?我想说我的洗牌方法已经移动了数组对象,第一个数组对象不再是“sam1”IE-


这就是错误所在-

AssertionError: expected [ Array(9) ] to equal true
我如何将两者进行比较以使其成为现实?或者有没有更好的方法来显示数组已被洗牌?

类似的内容?

assert.notEqual(组[0],“sam1”);

您可以在这里找到可用函数的列表
assert.equal()的第一个参数是比较的位置, 所以

应该是

assert.equal(comparison, 'message to display on failure');
知道数组是否被洗牌的更好方法是将结果中的每个元素与原始数组进行比较,比如

for(int i = 0; i < group.length; i++) {
    if (group[i] !== result[i]) {
        return false;
    }
}
for(int i=0;i
不过,根据你洗牌的方式,它有可能洗牌到相同的顺序


有关断言的更多详细信息,请参见。它看起来像是shuffle返回一个数组。因此,要检查
result
数组中的第一个元素是否与
group
数组中的第一个元素不同,您必须比较数组的前两个元素。如果要使用
assert
方法,必须这样做:


断言(结果[0]!=组[0],“测试失败时字符串”);


最简单的方法是比较前一个数组和后一个数组。这里不要使用
equal
,而是使用
deepEqual
来比较数组或对象

it('Shuffle should randomly move array items by their index', function(){
    let group = ["sam1","sam2","sam3","sam4","sam5","sam6","sam7","sam8","sam9"];
    let result = shuffle(group);
    assert.deepEqual(result, group);
  });
});
参考:

来源


我知道你没有问……但是如果你的洗牌真的是随机的,那么一件物品就有可能在它开始的地方结束。
for(int i = 0; i < group.length; i++) {
    if (group[i] !== result[i]) {
        return false;
    }
}
it('Shuffle should randomly move array items by their index', function(){
    let group = ["sam1","sam2","sam3","sam4","sam5","sam6","sam7","sam8","sam9"];
    let result = shuffle(group);
    assert.deepEqual(result, group);
  });
});
expect(result).to.have.members(group); // to check if all members there - doesn't care about the order
expect(result).to.not.eql(group); // to check if order has changed - there's a catch though; only 1 change on order (e.g. `shuffle` swapped **just** 2 members) is sufficient to this assertion to pass