我正在尝试使用 TDD 为 node.js 应用程序编写测试,但我不知道如何为我的 Products Routeget()函数编写 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,其中值是 a arrayof'products'与上面提到的模式一样。
我试过的:
// 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
有谁知道我怎样才能成功编写这个测试?
幕布斯6054654
函数式编程
相关分类