Chai response.body 总是空的 {}
Chai response.body is always empty {}
无论我的服务器实际上是什么 returns,当我断言 response.body:
时,Chai 总是给我这个异常
Uncaught AssertionError: expected {} to deeply equal 'test'
即使实际的服务器响应是 'test',而不是 {}:
这是我的测试:
const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('./test-server');
const should = chai.should();
chai.use(chaiHttp);
describe('GET /test', () => {
it('it should give test result', (done) => {
chai.request(server)
.get('/test')
.end((err, res) => {
console.log(err); // outputs null
console.log(res); // outputs normal-looking response
res.body.should.be.eql('test');
done();
});
});
});
这是我的服务器(测试-server.js):
const http = require('http');
const server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end('test');
});
module.exports = server;
server.listen(process.env.PORT || 8000);
console.log("Server running at http://localhost:8000/");
我做错了什么?
Content-Type: application/json
res.body
根据 Content-Type header
填充
test-server.js
const http = require('http');
const server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "application/json"});
var b = JSON.stringify({
name: 'asad',
class: 'paewe'
});
response.end(b);
});
module.exports = server;
server.listen(process.env.PORT || 8000);
console.log("Server running at http://localhost:8000/");
res.body
将包含已解析的 object
test.js
const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('./test-server');
const should = chai.should();
chai.use(chaiHttp);
describe('GET /test', () => {
it('it should give test result', (done) => {
chai.request(server)
.get('/test')
.end((err, res) => {
console.log(err); // outputs null
console.log(res); // outputs normal-looking response
console.log(res.body) // { name: 'asad', class: 'paewe' }
var checkObj = {
name: 'asad',
class: 'paewe'
}
res.body.should.be.eql(checkObj); // passes test
done();
});
});
});
-------------------------------------------- ---------------------------------------------- --------
Content-Type: text/plain
如果 Content-Type header 是 text/plain
,则响应 body 不会被解析为任何内容,但 res.text
将包含数据作为字符串
test-server.js
const http = require('http');
const server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end('test');
});
module.exports = server;
server.listen(process.env.PORT || 8000);
console.log("Server running at http://localhost:8000/");
test.js
const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('./test-server');
const should = chai.should();
chai.use(chaiHttp);
describe('GET /test', () => {
it('it should give test result', (done) => {
chai.request(server)
.get('/test')
.end((err, res) => {
console.log(err); // outputs null
console.log(res); // outputs normal-looking response
console.log(res.body) // {}
res.text.should.be.eql('test'); // passes test
done();
});
});
});
一些参考文献
无论我的服务器实际上是什么 returns,当我断言 response.body:
时,Chai 总是给我这个异常Uncaught AssertionError: expected {} to deeply equal 'test'
即使实际的服务器响应是 'test',而不是 {}:
这是我的测试:
const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('./test-server');
const should = chai.should();
chai.use(chaiHttp);
describe('GET /test', () => {
it('it should give test result', (done) => {
chai.request(server)
.get('/test')
.end((err, res) => {
console.log(err); // outputs null
console.log(res); // outputs normal-looking response
res.body.should.be.eql('test');
done();
});
});
});
这是我的服务器(测试-server.js):
const http = require('http');
const server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end('test');
});
module.exports = server;
server.listen(process.env.PORT || 8000);
console.log("Server running at http://localhost:8000/");
我做错了什么?
Content-Type: application/json
res.body
根据 Content-Type header
test-server.js
const http = require('http');
const server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "application/json"});
var b = JSON.stringify({
name: 'asad',
class: 'paewe'
});
response.end(b);
});
module.exports = server;
server.listen(process.env.PORT || 8000);
console.log("Server running at http://localhost:8000/");
res.body
将包含已解析的 object
test.js
const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('./test-server');
const should = chai.should();
chai.use(chaiHttp);
describe('GET /test', () => {
it('it should give test result', (done) => {
chai.request(server)
.get('/test')
.end((err, res) => {
console.log(err); // outputs null
console.log(res); // outputs normal-looking response
console.log(res.body) // { name: 'asad', class: 'paewe' }
var checkObj = {
name: 'asad',
class: 'paewe'
}
res.body.should.be.eql(checkObj); // passes test
done();
});
});
});
-------------------------------------------- ---------------------------------------------- --------
Content-Type: text/plain
如果 Content-Type header 是 text/plain
,则响应 body 不会被解析为任何内容,但 res.text
将包含数据作为字符串
test-server.js
const http = require('http');
const server = http.createServer(function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.end('test');
});
module.exports = server;
server.listen(process.env.PORT || 8000);
console.log("Server running at http://localhost:8000/");
test.js
const chai = require('chai');
const chaiHttp = require('chai-http');
const server = require('./test-server');
const should = chai.should();
chai.use(chaiHttp);
describe('GET /test', () => {
it('it should give test result', (done) => {
chai.request(server)
.get('/test')
.end((err, res) => {
console.log(err); // outputs null
console.log(res); // outputs normal-looking response
console.log(res.body) // {}
res.text.should.be.eql('test'); // passes test
done();
});
});
});
一些参考文献