与椅子相关的错误消息:“AssertionError: expected undefined to deep equal
Chai-related error message: "AssertionError: expected undefined to deeply equal"
我写了一个函数,当给定一个对象列表和一个Id,返回相同的列表,但对应的对象标记为活动(所有其他对象都不应该处于活动状态)。
const list = [
{ id: 1, active: false },
{ id: 2, active: false },
{ id: 3, active: true },
{ id: 4, active: false }
];
function markActive(list, value) {
list.forEach((id) => {
if (id.active = (id.id === value)) {
return true;
} else {
return false;
}
});
}
markActive(list, 2);
console.log(list)
的回报:
[ { id: 1, active: false },
{ id: 2, active: false },
{ id: 3, active: false },
{ id: 4, active: true } ]
它的工作就像一个魅力,除了当我运行"npm run [filename]"我得到一个错误信息:
Running Tests for [filename].
------------
[ { id: 1, active: false },
{ id: 2, active: false },
{ id: 3, active: false },
{ id: 4, active: true } ]
markActive
1) Case 1 (Given Sample)
2) Case 2 (String IDs)
0 passing (16ms)
2 failing
1) markActive Case 1 (Given Sample):
AssertionError: expected undefined to deeply equal [ { id: 1,
active: false },
{ id: 2, active: true },
{ id: 3, active: false },
{ id: 4, active: false } ]
at Function.assert.deepEqual
(node_modules/chai/lib/chai/interface/assert.js:216:32)
at Context.it (tests/test_02.js:23:12)
2) markActive Case 2 (String IDs):
AssertionError: expected undefined to deeply equal [ { id: '1',
active: false },
{ id: '2', active: true },
{ id: '3', active: false },
{ id: '4', active: false } ]
at Function.assert.deepEqual
(node_modules/chai/lib/chai/interface/assert.js:216:32)
at Context.it (tests/test_02.js:40:12)
知道我做错了什么吗?下面是设置测试的代码:
const chai = require("chai");
const sinon = require("sinon");
const assert = chai.assert;
const markActive = require("../answers/02.js");
describe("markActive", () => {
it("Case 1 (Given Sample)", () => {
var list = [
{ id: 1, active: false },
{ id: 2, active: false },
{ id: 3, active: true },
{ id: 4, active: false }
];
var newList = markActive(list, 2);
var targetList = [
{ id: 1, active: false },
{ id: 2, active: true },
{ id: 3, active: false },
{ id: 4, active: false }
];
assert.deepEqual(newList, targetList);
});
it("Case 2 (String IDs)", () => {
var list = [
{ id: "1", active: false },
{ id: "2", active: false },
{ id: "3", active: true },
{ id: "4", active: false }
];
var newList = markActive(list, "2");
var targetList = [
{ id: "1", active: false },
{ id: "2", active: true },
{ id: "3", active: false },
{ id: "4", active: false }
];
assert.deepEqual(newList, targetList);
});
});
你的函数没有返回任何东西,所以你试图设置结果的任何变量都将被设置为undefined
。
要解决这个问题,只需在函数末尾添加一个return
语句。
function markActive(list, value) {
list.forEach((id) => {
if (id.active = (id.id === value)) {
return true;
} else {
return false;
}
});
return list; // return the updated list
}
注意:值得一提的是,因为数组是被引用的,所以要就地修改值。这就是为什么您在函数外部定义的数组仍然具有更新的结果,即使您没有记录返回值。如果要在同一列表上多次运行markActive()
函数,这可能会产生意想不到的副作用。如果你想要返回一个新的列表,看看Javascript中复制和深度复制数组的方法。
相关文章:
- console.assert失败时中断chrome调试器
- 如何在JavaScript中使用Mocha和assert.throws测试参数的值
- Node.js assert.deep等于2个值
- 使用自定义断言帮助程序对cli扩展assert进行ember-cli扩展
- Node.js assert.throws with async functions (Promises)
- JSX: Expected corresponding closing tag for <div>
- Javascript, error meassge expected object
- assert.throws on asynchronous method
- Nitghwatch.js -- assert.hidden 在页面的一部分上
- 带有nodejs的mocha为assert(false)而不是error断言挂起/超时
- 浏览器中的Mocha:如何使用chai.assert获取报告
- 一个不需要输入错误消息的assert函数
- jsLint Expected {
- 在node.js中启用/禁用assert语句的简单方法
- Function Expected In IE7 & IE8
- Onclick doesn't work as expected on tr
- CS1002: ; expected
- 为什么Node.js Assert没有notOk()方法?
- Javascript: Jasmine Error: Expected { 0 : HTMLNode, length :
- assert failed: InvalidValueError: setContent:不是字符串