[英]chai: expect object to have array of objects like (in format)
我正在尝试使用 TDD 为 node.js 应用程序编写测试,但我不知道如何为我的 Products Route get()
function 编写 chai expect 测试。
// productsController.js
let items = [
{
id: 1,
name: 'Product 1',
description: 'Product1 description',
price: 19.00
}
];
module.exports = {
get(_, res) {
res.json({items});
}
};
我已经阅读了几次文档,但我似乎不太明白如何测试响应 object 是否预期包含关键items
,其中值是具有上述架构的'products'
array
以上。
我试过的:
// 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
有谁知道我怎样才能成功编写这个测试?
我想到了!
// products.test.js
const chai = require('chai');
chai.use(require('chai-json-schema'));
const expect = chai.expect;
const { get } = require('../../routes/productsController');
let req = {
body: {},
params: {},
};
const res = {
jsonCalledWith: {},
json(arg) {
this.jsonCalledWith = arg
}
}
let productSchema = {
title: 'productSchema',
type: 'object',
required: ['id', 'name', 'description', 'price'],
properties: {
id: {
type: 'number',
},
name: {
type: 'string'
},
description: {
type: 'string',
},
price: {
type: 'number',
},
}
};
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');
expect(res.jsonCalledWith.items).to.be.an('array');
res.jsonCalledWith.items.forEach(product => expect(product).to.be.jsonSchema(productSchema));
});
});
});
原来插件chai-json-schema允许验证 json 对象以满足预定义的模式。
我认为您收到错误是因为预期的 output 是一个数组,以完成您需要的测试:
it('Check route list all users', (done) => {
api.get('/usuarios')
.set('Accept', 'application/json; charset=utf-8')
.expect(200)
.end((err, res) => {
expect(res.body).to.be.an('array');
expect(res.body.length).to.equal(1);
done();
});
});
这是一个将数组作为 json 响应返回的示例。
下面是对来自路由的 object User
实例的相同测试:
it('Check get by id return 200', (done) => {
api.get('/usuarios/1')
.set('Accept', 'application/json; charset=utf-8')
.expect(200)
.end((err, res) =>{
expect(res.body).to.have.property('nome');
expect(res.body.nome).to.equal('abc');
expect(res.body).to.have.property('email');
expect(res.body.email).to.equal('a@a.com');
expect(res.body).to.have.property('criacao');
expect(res.body.criacao).to.not.equal(null);
expect(res.body).to.have.property('atualizado');
expect(res.body.atualizado).to.not.equal(null);
expect(res.body).to.have.property('datanascimento');
expect(res.body.datanascimento).to.not.equal(null);
expect(res.body).to.have.property('username');
expect(res.body.username).to.equal('abcdef');
expect(res.body).to.have.property('statusmsg');
expect(res.body.statusmsg).to.equal('status');
expect(res.body).to.have.property('genero');
expect(res.body.genero).to.equal('M');
expect(res.body).to.have.property('descricao');
expect(res.body.descricao).to.equal('descricao');
done();
});
});
我的例子我使用mocha
和chai
和supertest
。
希望对您有所帮助,如果您需要更多说明,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.