Here is my controller
function test(){
console.log('trace1');
myService.useService('someStuff')
.then(function(data){
console.log('trace2');
})
.catch(function(error){
console.log('trace3');
});
}
Here is my test suite
describe('test controller promise', function () {
beforeEach(inject(function(
$controller,
$q,
_$rootScope_,
$location,
$anchorScroll,
_myService_,
$window
) {
$rootScope = _$rootScope_;
myService = _myService_;
var testDeferred = $q.defer();
testDeferred.reject('some_error_message');
spyOn(myService, 'useService').and.callFake(function(){
console.log('trace 5');
return testDeferred.promise
});
myController = $controller('myController', {
$rootScope: $rootScope,
myService: myService,
$location: $location,
$anchorScroll: $anchorScroll,
$window
});
$rootScope.$apply();
}));
it('test promise', function () {
myController.test();
});
I want the called .thenor .catchinvoked after calling the mock-service (ideally I need to see trace3for printing, as used reject), but we see that it is not called, as trace2and trace3will not be printed, but it is curious that Fake function is a challenge because how trace5is printed
I tried callFake, returnValueand using both rejectand resolve, all of them do not lead to a call .thenor .catch. What am I missing?
source
share