作者:BYREAD315794103 | 来源:互联网 | 2024-09-25 07:35
我不确定为什么您的方法行不通,但是我通常使用该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;
};
}