Jasmine Nodetestingsubprocess
刚刚写了一个模块,抓住生产箱的当前IOStat,以确定是否过度工作。
有趣的问题 – 没有真正的方法来testing与Jasmine的callback函数; 至less,我发现了一个。
间谍是我想到的第一件事,但由于它实际上是直接调用函数,所以这是不可能的。 当然,我总是可以把这个函数定义为一个var并且做一个间谍。
proc.exec('iostat -c | tail -n2', function(err, out, s){ if(err) throw err; misc(); }
我想testing这个的唯一原因 – 是因为我正在开发一个没有所有这些命令的Windows机器,所以我想拦截它并抛出预期的结果。
你必须窥探proc.exec
并从spy.mostRecentCall.args
数组中获取callback函数,然后通过你自己调用:
spyOn(proc, 'exec') //run your code proc.exec.mostRecentCall.args[1](true)
由于这不是很方便,你可以使用sinon ,在那里你可以创build一个存根,用stub.callsArgWith(index, args)
自动调用callbackstub.callsArgWith(index, args)
。
sinon.stub(proc, 'exec').callsArgWith(1, true); //run your code