2013-12-11 35 views
0

我是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; 
+0

好,间谍没有得到正确设置 - 我不知道为什么。如果间谍正在工作,函数的实际代码将不会运行。间谍,默认情况下,取代他们观看的功能,而不是观看和报告。如果你想让函数正常运行,你需要做一些事情,比如'spyOn(child_process,'spawn')。andCallThrough();' – user1618143

回答

0

哦,我看到了问题。您创建nndb,nndbnndb.spawn等于nndb.child_process.spawn。然后你在nndb.child_process.spawn上设置了一个间谍,但是因为nndb.spawn已经被复制掉了,所以间谍不适用于两者。当nndb.scrapeForXYZ调用nndb.spawn时,间谍不受影响。

试试这个:

it("spawns child process", function() { 

    var nndb = new Nndb(); 
    var child_process = nndb.child_process; 
    spyOn(nndb, "spawn"); 

    //... 
     expect(nndb.spawn).toHaveBeenCalledWith(//... 

} 
+0

嗨,你观察到的是正确的。它的轻微修改是我仍然需要监视child_process而不是nndb。 Nndb是测试主题。删除行“this.spawn = this.child_process.spawn;”后从nndb构造函数中添加“spyOn(child_process,”spawn“).CallThrough();”通过了测试。谢谢您的回答。现在我必须弄清楚如何测试9小时前产生的child_process - arunbakt中的child.stdout.on和child.stderr.on方法 – arunbakt