我正在尝试为一个脚本编写一些单元测试,但是我似乎无法让YQL和nock一起工作。使用nock我可以毫无问题地模拟http请求,如下面的示例所示,但是yql的测试失败了,无论我使用什么,yql总是不会产生任何结果(我也在查询中尝试了xpath )。
var http = require('http'),
nock = require('nock'),
yql = require('yql')
/**
* Mock request & test with http
*/
var api = nock("http://example.tld")
.get("/foobar/")
.reply(200, "<html><body><table><tr><td class=\"tablebody\"><a href=\"#\">link</a></td></tr></table></body></html>")
http.get("http://example.tld/foobar/", function(resp){
var str = "";
resp.on("data", function(data){ str+=data})
resp.on("end", function(){
console.log("Got Result: ", str)
})
})
/**
* Mock request and test with YQL
*/
var api = nock("http://example.tld")
.get("/foobar/")
.reply(200, "<html><body><table><tr><td class=\"tablebody\"><a href=\"#\">link</a></td></tr></table></body></html>")
var query = new yql('select * from html where url="http://example.tld/foobar/"')
query.exec(function(err, results){
console.log(results)
})我想知道这是一个头文件的问题,但我尝试过的任何东西都没有在yql上产生任何结果。-非常感谢你的帮助,戴蒂
发布于 2015-08-28 19:20:41
似乎nock双重转义了url,这对我来说是个问题,因为url包含一个转义的url。
最后,我使用了允许我存根yql.exec的proxyquire。所以我的yql单元测试现在看起来是这样的:
var json = JSON.parse(fs.readFileSync('expectedResult.json', 'utf8'))
yqlStub = function(query){
return {
exec: function(fn){
fn(null, json)
}
}
},
myModule = proxyrequire('lib/myModule.js', {'yql', yqlStub})
myModule.run("http://example.tld", function(err, result){
//test result here
})https://stackoverflow.com/questions/32248013
复制相似问题