这是我的控制器
function test(){
console.log('trace1'); // this gets print on console
myService.useService('someStuff')
.then(function(data){
console.log('trace2'); // this doesn't get print
})
.catch(function(error){
console.log('trace3'); // this doesn't get print
});
}
这是我的测试套件
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'); // this gets called
return testDeferred.promise
});
myController = $controller('myController', {
$rootScope: $rootScope,
myService: myService,
$location: $location,
$anchorScroll: $anchorScroll,
$window
});
$rootScope.$apply();
}));
it('test promise', function () {
myController.test();
// we see trace1 and trace5 gets called but not trace2 and trace3
});
我希望在调用模拟服务后调用.then
或.catch
(理想情况下,我应该看到trace3
打印,因为我使用了reject
),但是我们发现,由于trace2
并且trace3
没有打印,因此无法调用它,但奇怪的是Fake函数确实被调用,因为trace5
得到印刷
我已尝试callFake
,returnValue
并同时使用reject
和resolve
,所有这些都不会导致.then
或{{ 1}}被召唤。我错过了什么?