【发布时间】:2019-07-24 02:47:18
【问题描述】:
我有一个 index.js 文件,它正在实现一个 forEach 帮助器,如下所示:
var images = [
{ height: 10, width: 30 },
{ height: 20, width: 90 },
{ height: 54, width: 32 }
];
var areas = [];
images.forEach(function(image) {
return areas.push(image.height * image.width);
});
console.log(areas);
module.exports = images;
我知道解决方案有效,您知道解决方案有效,它有效。
然后在我的 test.js 文件中:
const chai = require("chai");
const images = require("./index.js");
const expect = chai.expect;
describe("areas", () => {
it("contains values", () => {
expect([]).equal([300, 1800, 1728]);
});
});
当我运行 npm test 时,我继续收到 AssertionError。
我将包含package.json 文件:
{
"name": "my_tests",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha"
},
"keywords": [],
"license": "MIT",
"dependencies": {
"chai": "4.2.0",
"mocha": "6.0.2"
}
}
我像这样重构了我的test.js 文件:
const chai = require("chai");
const areas = require("./index.js");
const expect = chai.expect;
describe("areas", () => {
it("contains values", () => {
const areas = [];
expect(areas).equal([300, 1800, 1728]);
});
});
仍然收到 AssertionError:
AssertionError: expected [] to equal [ 300, 1800, 1728 ]
+ expected - actual
-[]
+[
+ 300
+ 1800
+ 1728
+]
【问题讨论】:
-
实际错误是什么?您是要导出
areas而不是images? -
@JackBashford,
areas输出[300, 1800, 1728]等我说expect([]).equal([300, 1800, 1728]);,它应该是通过了。 -
但是
areas不在您的test文件中 - 您正在从index导出images,而不是areas。 -
@JackBashford,我也试过了,但我仍然收到 AssertionError。
标签: javascript arrays testing ecmascript-6 foreach