【发布时间】:2020-07-13 00:25:36
【问题描述】:
我正在尝试使用 TDD 为 node.js 应用程序编写测试,但我不知道如何为我的 Products Route get() 函数编写 chai expect 测试。
// productsController.js
let items = [
{
id: 1,
name: 'Product 1',
description: 'Product1 description',
price: 19.00
}
];
module.exports = {
get(_, res) {
res.json({items});
}
};
我已经阅读了几次文档,但我似乎不太明白如何测试响应对象是否应包含键 items,其中值是 array 的 @987654325 @ 与上面提到的架构类似。
我尝试过的:
// products.test.js
const { get, getById } = require('../../routes/productsController');
const res = {
jsonCalledWith: {},
json(arg) {
this.jsonCalledWith = arg
}
}
describe('Products Route', function() {
describe('get() function', function() {
it('should return an array of products ', function() {
get(req, res);
expect(res.jsonCalledWith).to.be.have.key('items').that.contains.something.like({
id: 1,
name: 'name',
description: 'description',
price: 18.99
});
});
});
});
但是,我收到此错误:
AssertionError: expected { Object (items) } to be an array
有人知道我怎样才能成功编写这个测试吗?
【问题讨论】:
标签: javascript node.js mocha.js tdd chai