2013-12-14 105 views
0

当我试图执行jasmine-node测试时,我得到下面的错误。节点子进程茉莉花错误

我想测试下面的JavaScript

var StringDecoder = require('string_decoder').StringDecoder; 
var decoder = new StringDecoder('utf8'); 

Nndb = function(child_process) { 
    this.child_process = child_process; 
}; 

Nndb.prototype.invokePhantomProcess = function(phantomScriptToRun, offSet) { 

    var child = this.child_process.spawn('phantomjs', [phantomScriptToRun, offSet]); 
    child.stdout.on('data',function(data){ 

     console.log(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; 

与下面的测试规范:

require('../src/TDDQuestion.js'); 

describe("Verify", function() { 

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

     var stdout = { 
      on: function() {} 
     }; 

     var child_process = { 
      stdout: stdout, 
      spawn: function(){return this;} 
     }; 

     spyOn(child_process,"spawn").andReturn(child_process); 
     spyOn(child_process, "stdout").andReturn(stdout); 

     var nndb = new Nndb(child_process); 

     nndb.invokePhantomProcess('../src/TDDQuestion.js', 0); 
     expect(child_process.spawn).toHaveBeenCalledWith('phantomjs',['../src/TDDQuestion.js',0]); 

    }); 


}); 

,并得到错误:

enter Stacktrace: 
TypeError: Object function() { 
spyObj.wasCalled = true; 
spyObj.callCount++; 
var args = jasmine.util.argsToArray(arguments); 
spyObj.mostRecentCall.object = this; 
spyObj.mostRecentCall.args = args; 
spyObj.argsForCall.push(args); 
spyObj.calls.push({object: this, args: args}); 
return spyObj.plan.apply(this, arguments); 
} has no method 'on' 
    at Nndb.invokePhantomProcess 

(/Users/.../src/TDDQuestion.js:11:18) 
at null.<anonymous> (/Users/.../spec/TDDQuestionSpec.js:21:14) 

我看到的方法” '出现在测试规范的对象标准输出中。

我还观察到,从

child.stdout.on //returns a function causing the error 

改变TDDQuestion.js行至

child.stdout().on //returns the stdout object with the 'on' method 

时解决问题。

回答

0

删除stdout上的间谍并将其视为属性(它是)解决此问题。间谍应用只适用于方法上的功能属性。

我在这里粘贴修正的TDDSpecQuestion.js。

require('../src/TDDQuestion.js'); 

describe("Verify", function() { 

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


     var stdout = { 
      on: function() {} 
     }; 
     var stderr = { 
      on: function() {} 
     } 

     var child_process = { 
      stderr: stderr, 
      stdout: stdout, 
      spawn: function(){return this;}, 
      on: function(){} 
     }; 
     debugger; 
     spyOn(child_process,"spawn").andReturn(child_process); 
     var nndb = new Nndb(child_process); 

     nndb.invokePhantomProcess('../src/TDDQuestion.js', 0); 
     expect(child_process.spawn).toHaveBeenCalledWith('phantomjs',['../src/TDDQuestion.js',0]); 

    }); 


});