【发布时间】:2013-01-19 07:13:13
【问题描述】:
在 node.js 中,我无法让 superagent 和 nock 一起工作。如果我使用请求而不是超级代理,它会完美运行。
这是一个简单的例子,superagent 无法报告模拟数据:
var agent = require('superagent');
var nock = require('nock');
nock('http://thefabric.com')
.get('/testapi.html')
.reply(200, {yes: 'it works !'});
agent
.get('http://thefabric.com/testapi.html')
.end(function(res){
console.log(res.text);
});
res 对象没有“文本”属性。出了点问题。
现在如果我使用请求做同样的事情:
var request = require('request');
var nock = require('nock');
nock('http://thefabric.com')
.get('/testapi.html')
.reply(200, {yes: 'it works !'});
request('http://thefabric.com/testapi.html', function (error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body)
}
})
模拟的内容显示正确。
我们在测试中使用了 superagent,所以我宁愿坚持下去。有谁知道如何让它工作?
非常感谢, 泽维尔
【问题讨论】:
标签: javascript node.js request superagent nock