我是nodejs和jasmine的新手。在学习javascript/node和jasmine的小项目上工作。我试图窥探节点中的对象child_process并查看指定参数调用了“spawn”方法。使用jasmine监听节点child_process
茉莉花错误报告说,当调用调用spawn方法的对象(本例中是Nndb)被调用时,从不会调用spawn。但实际的工作是由子进程执行的,因为我看到了在控制台中打印的结果。
这里是失败我看到运行茉莉节点脚本时:
Failures:
1) scrape for xyz spawns child process Message: Expected spy spawn to have been called with [ '../src/scrape_nndb.js', 0 ] but it was >never called. Stacktrace: Error: Expected spy spawn to have been called with [ '../src/scrape_nndb.js', 0 ] but >it was never called. at null. (/Users/arun.bakt/skunkWorks/scraping/spec/nndb_spec.js:30:41) at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
Finished in 6.024 seconds 1 test, 1 assertion, 1 failure, 0 skipped"
茉莉花测试文件吧:
require('../src/nndb.js');
describe("scrape for XYZ", function() {
var child = require('child_process');
it("spawns child process", function() {
var nndb = new Nndb();
var child_process = nndb.child_process;
spyOn(child_process, "spawn");
runs(function(){
flag= false;
nndb.scrapeForXYZ('../src/scrape_nndb.js', 0);
setTimeout(function() {
flag = true;
},6000)
});
waitsFor(function(){
return flag;
}, "Scraping done", 6000);
runs(function(){
expect(child_process.spawn).toHaveBeenCalledWith('../src/scrape_nndb.js',0);
});
});
});
正在被测试下面的文件nndb.js:
var StringDecoder = require('string_decoder').StringDecoder;
var decoder = new StringDecoder('utf8');
var child_process = require('child_process');
Nndb = function() {
this.child_process = child_process;
this.spawn = this.child_process.spawn;
};
Nndb.prototype.scrapeForXYZ = function(phantomScriptToRun, offSet) {
var child = this.spawn('phantomjs', [phantomScriptToRun, offSet]);
child.stdout.on('data',function(data){
console.log("MATCH "+decoder.write(data));
});
child.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
child.on('exit', function (code) {
console.log('child process exited with code ' + code);
});
return 0;
};
exports.Nndb = Nndb;
好,间谍没有得到正确设置 - 我不知道为什么。如果间谍正在工作,函数的实际代码将不会运行。间谍,默认情况下,取代他们观看的功能,而不是观看和报告。如果你想让函数正常运行,你需要做一些事情,比如'spyOn(child_process,'spawn')。andCallThrough();' – user1618143