Chai 无法读取 属性 'should' of null
Chai Cannot read property 'should' of null
使用 knex. I'm also referencing this https://mherman.org/blog/test-driven-development-with-node/
对单个项目进行单元测试时出现以下错误
它渲染 api/users/1 成功。所以不确定为什么它说空。
GET /api/users/:id
Should return a single show :
Uncaught TypeError: Cannot read property 'should' of null
at should (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
})
});
})
您正在从 chai
而不是 should
导入 expect
。您可以更改:
response.should.have.status(200);
到 expect(response).to.have.status(200);
(对所有的 should 语句执行此操作)
或通过更改导入:
import { expect } from 'chai';
到 import { should } from 'chai';
希望对您有所帮助!
使用 knex. I'm also referencing this https://mherman.org/blog/test-driven-development-with-node/
对单个项目进行单元测试时出现以下错误它渲染 api/users/1 成功。所以不确定为什么它说空。
GET /api/users/:id Should return a single show : Uncaught TypeError: Cannot read property 'should' of null at should (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
})
});
})
您正在从 chai
而不是 should
导入 expect
。您可以更改:
response.should.have.status(200);
到 expect(response).to.have.status(200);
(对所有的 should 语句执行此操作)
或通过更改导入:
import { expect } from 'chai';
到 import { should } from 'chai';
希望对您有所帮助!