req.body 未定义 测试时



我正在使用摩卡和柴对我的快速应用程序进行单元测试

在测试文件中,我想使用req.body来解析对象,使其仅在测试结果中未定义。

我在服务器(app.js)中已经需要正文解析器

我错过了什么,我确实得到了回应。这是调用返回对象的路由res.json(response)

//Testing File
let chai = require('chai');
let expect  = require('chai').expect;
let request = require('request');
let app = require('../app');
let should = chai.should();

describe('/GET wallet', () => {
it('object should have these properties', (done) => {
request('http://localhost:3000/theFunction' ,function(error,res,req) {
expect(res.body).to.have.property('name')

done();
});
});
});

看起来您正在发出获取请求。

const options = 
{
uri: 'http://localhost:3000/theFunction',
method: 'POST',
json: { "name": stackoverflow" } // json data you want to send. this will be populated as req.body
};
request(options, function (error, res, body) {
expect(res.body).to.have.property('name')
done();
});

最新更新