【发布时间】:2018-06-16 16:26:27
【问题描述】:
我正在尝试来自我的 test.js 的以下发布请求,但可惜我在等待 2 小时以来一直在尝试,等待我的测试成功但它没有成功。
请用nock找到下面的代码。
beforeEach(() => {
var payload = {
channel: 'Cricket',
codes: 'c--HOjyWVww:APA'
};
scope = nock('http://localhost:3000')
.post('/channel/subscribe', payload)
.reply(200, { status: 200 });
});
request.post('http://localhost:3000/channel/subscribe', function(err, body) {
console.log(err, null);
});
it('Subscribing to channel', function(done) {
request.post({
url: 'http://localhost:3000/channel/subscribe',
headers: {
'accept': 'application/json',
'content-Type': 'application/x-www-form-urlencoded'
},
mode: 'no-cors',
body: JSON.stringify({
channel: "Cricket",
codes: "c--HOjyWVww:APA"
})
}, function(err, response) {
console.log(request.payload);
console.log("+response status code:", response.statusCode);
console.log(response.failureCount);
if (err) {
throw err; // => Error: Nock: No match for request POST http://foobar.com/ {"foo":"biz"}
}
done();
})
【问题讨论】:
标签: node.js unit-testing request hapijs nock