我正在发送API请求并获得有效的响应。这是我的路线(/测试):
module.exports = {
test: function(req, res) {
var http = require('http');
var nock = require('nock');
var request = require('request');
nock('http://greetings_api:3000')
.defaultReplyHeaders({
'Content-Type': 'application/json'
})
.post('/greeting', {'language': 'es' })
.reply(200, 'Hola');
var options = {
uri: 'http://greetings_api:3000/greeting',
hostname: 'greetings_api',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'language': 'es'
}
};
request(options, function (error, response, body) {
console.log(response.body);
res.send(response.body + ' tester');
});
}
};
这是我的考试:
const expect = require('chai').expect;
const { test } = require('../../routes/test');
let req = {
body: {},
};
let res = {
sendCalledWith: '',
send: function(arg) {
this.sendCalledWith = arg;
}
};
describe('Test Route', function() {
describe('Test() function', function() {
it('Should welcome us', function() {
test(req, res);
expect(res.sendCalledWith).to.contain('Hola tester');
});
})
});
我收到以下错误:
1) Test Route
Test() function
Should welcome us:
AssertionError: expected '' to include 'Hola tester'
at Context.<anonymous> (/tests/routes/app.test.js:20:43)
测试似乎没有正确地从路由中获取响应。我怎样才能使测试与Nock一起使用?