chai:期望对象具有对象数组,例如(格式)

我正在尝试使用 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


有谁知道我怎样才能成功编写这个测试?


largeQ
浏览 95回答 2
2回答

幕布斯6054654

我想到了!// products.test.jsconst 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 对象以满足预定义的模式。

函数式编程

我认为您收到错误是因为预期的输出是一个数组,以完成您需要的测试: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();            });    });我的例子是我使用mochaandchai和supertest。希望对您有所帮助,如果您需要更多说明,请告诉我。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript