我正在尝试使用TDD为node.js应用程序编写测试,但我不知道如何为我的产品路线get()
函数编写预期的测试。
// productsController.js
let items = [
{
id: 1,
name: 'Product 1',
description: 'Product1 description',
price: 19.00
}
];
module.exports = {
get(_, res) {
res.json({items});
}
};
我已经阅读了几次文档,但是我似乎不太明白如何测试响应对象是否应该包含键items
,其中值是{{1} array
中的},其格式与上述类似。
我尝试过的:
'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
});
});
});
});
有人知道我如何成功编写此测试吗?
答案 0 :(得分:0)
我认为您收到错误是因为预期的输出是一个数组,要完成您的测试,您需要:
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响应的示例。
以下是对来自路由的对象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
。
希望有帮助,如果您需要更多说明,请告诉我。
答案 1 :(得分:0)
我知道了!
// 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对象满足预定义的模式。