我不确定为什么您的方法不起作用,但是我通常使用该
spyOn函数来完成。像这样:
describe('Testing remote call returning promise', function() { var myService; beforeEach(module('app.myService')); beforeEach(inject( function(_myService_, myOtherService, $q){ myService = _myService_; spyOn(myOtherService, "makeRemoteCallReturningPromise").and.callFake(function() { var deferred = $q.defer(); deferred.resolve('Remote call result'); return deferred.promise; }); } it('can do remote call', inject(function() { myService.makeRemoteCall() .then(function() { console.log('Success'); }); }));
还要记住,您将需要
$digest调用要调用的
then函数。请参阅$q文档的“ 测试”部分。
- - - 编辑 - - -
在仔细查看您在做什么之后,我认为我在您的代码中看到了问题。在中
beforeEach,您将设置
myOtherServiceMock为一个新对象。将
$provide永远不会看到这个参考。您只需要更新现有参考:
beforeEach(inject( function(_myService_, $q){ myService = _myService_; myOtherServiceMock.makeRemoteCallReturningPromise = function() { var deferred = $q.defer(); deferred.resolve('Remote call result'); return deferred.promise; }; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)