茉莉花间谍没有叫

时间:2014-05-27 01:45:04

标签: javascript jasmine spy

我是茉莉花间谍的新手。

这是我的测试:

spyOn(gapi, 'ready').andCallThrough();
inject(function(_endpointService_) {
  endpointService = _endpointService_;
});        
var _doSteps = gapi.ready.mostRecentCall.args[0];
var wrapper = {_doSteps: _doSteps};
spyOn(wrapper, '_doSteps');
gapi.ready(); //calls _doSteps through promise in service.
//_doSteps();
expect(wrapper._doSteps).wasCalled(); //gives error - not called.

在我的endpointService中,我有:

gapi.ready($endpointService._doSteps);
return $endpointService;

和我的_doSteps方法:

_doSteps: function(){
   console.log('in dosteps!');
 },

它记录在dosteps中!'在间谍抱怨_doSteps没有被召唤之前。我该怎么做?

1 个答案:

答案 0 :(得分:1)

你的spyOn()函数返回一个间谍对象 - 所以我不确定你为什么使用mostRecentCall和包装器?

var gapi = new Gapi(); // must create a variable to spy on.
var readySpy = spyOn(gapi, 'ready').andCallThrough();
// .. something that calls gapi.ready here
expect(readySpy).toHaveBeenCalled();