我有一个foo.js
看起来像这样的文件:
var exec = require('child_process').exec
...
function start(){
...
exec('open -g http://localhost:<port>'); // opens a browser window
}
// EOF
我想测试当我调用该start()
函数时,会打开一个浏览器窗口。理想情况下,我想使用 Sinon 来存根exec
(这样我们在自动化测试期间实际上不会打开浏览器窗口),并断言它exec
被调用了。我尝试了很多方法,但都没有奏效。例如在foo_test.js
:
var subject = require('../lib/foo');
describe('foo', function(){
describe('start', function(){
it('opens a browser page to the listening address', function(){
var stub = sinon.stub(subject, 'exec', function(){
console.log('stubbed exec called');
}); // fails with "TypeError: Attempted to wrap undefined property exec as function"
});
});
});
我该怎么做呢?