为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

largeQ 2022-11-11 16:06:34
我正在尝试使用 TDD 为 node.js 应用程序编写测试,但我不知道如何为我的 Products Routeget()函数编写 chai expect 测试。// productsController.jslet 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.jsconst { 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有谁知道我怎样才能成功编写这个测试?
查看完整描述

2 回答

?
幕布斯6054654

TA贡献1876条经验 获得超7个赞

我想到了!


// 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 对象以满足预定义的模式。


查看完整回答
反对 回复 2022-11-11
?
函数式编程

TA贡献1807条经验 获得超9个赞

我认为您收到错误是因为预期的输出是一个数组,以完成您需要的测试:


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。


希望对您有所帮助,如果您需要更多说明,请告诉我。


查看完整回答
反对 回复 2022-11-11
  • 2 回答
  • 0 关注
  • 93 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信