Jasmine spyOn没有使用angularJS

时间:2016-01-20 02:08:54

标签: javascript angularjs jasmine 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    
      });
}

这是我的测试套件

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得到印刷

我已尝试callFakereturnValue并同时使用rejectresolve,所有这些都不会导致.then或{{ 1}}被召唤。我错过了什么?

0 个答案:

没有答案