tes*_*ing 5 javascript jasmine angularjs karma-jasmine
这是我的控制器
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
});
}
Run Code Online (Sandbox Code Playgroud)
这是我的测试套件
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
});
Run Code Online (Sandbox Code Playgroud)
我想无论是.then或.catch模拟服务被称为(理想情况下,我应该看到后调用trace3,因为我用来获取打印reject),但我们看到,它不会因为所谓的trace2和trace3没有得到打印的,但什么是好奇的是自从trace5被打印以来,Fake函数确实被调用
我试过了callFake,returnValue并且同时使用reject和resolve,它们都不会导致.then或.catch不会被调用。我想念什么?
| 归档时间: |
|
| 查看次数: |
376 次 |
| 最近记录: |