柴无法读取属性“应该”为空

时间:2019-04-09 20:22:37

标签: javascript mocha chai knex.js

使用knex对单个项目进行单元测试时,出现以下错误。我也引用了这个https://mherman.org/blog/test-driven-development-with-node/

它成功渲染api / users / 1。所以不知道为什么说空。

  

获取/ api / users /:id          应该返回一个节目:        未被捕获的TypeError:无法读取null的属性“应该”         应该(tests / users.spec.js:59:26)

users.spec.js

import chai from 'chai';
import { expect } from 'chai';
import chaiHttp from 'chai-http';
import { assert } from 'assert'
import users from '../routes/users';
import request from 'supertest';


describe('GET /api/users/:id ', () =>{
    it('Should return a single show ', (done)=> {
       chai.request(users)
            .get('/users/1')
            .set('Accept', 'application/json')
            .end((response) => {
                // error begins here
                response.should.have.status(200);
                response.should.be.json; 
                response.body.should.be.a('object');
                response.body.should.have.property('id');
                response.body.name.should.equal(1);
                done()
            })          

    });
})

routes / users.js

users.get('/users/:id', (req, res) => {
    return knex('users').where({id: req.params.id})
        .then( (user) => {
            res.json({
                user: user
            })
        });
})

1 个答案:

答案 0 :(得分:1)

您要从expect而不是chai导入should。您可以更改:

response.should.have.status(200);expect(response).to.have.status(200);(对您所有的should语句都这样做)

或导入应通过更改:

import { expect } from 'chai';import { should } from 'chai';

希望有帮助!