如何在业力茉莉花中的$ ionicPlatform.ready之后调用函数

时间:2017-01-16 16:17:36

标签: javascript angularjs ionic-framework jasmine

有人可以解释我在茉莉花测试中如何在$ ionicPlatform.ready之后进行调用吗?

这是我的控制器代码的简单示例:

function myController($ionicPlatform, myService) {
    $ionicPlatform.ready(function() {
        myService
        .get()
        .then(function() {
            // success
        })
        .catch(function() {
            // error
        });
    });
}

以下是测试:

function myControllerTest() {
    var $controller,
        $ionicPlatform,
        $q,
        $rootScope,
        controller,
        deferred,
        myServiceMock;

    beforeEach(module('my_module'));
    beforeEach(inject(function(_$controller_, _$ionicPlatform_, _$q_, _$rootScope_) {
        $controller = _$controller_;
        $ionicPlatform = _$ionicPlatform_;
        $q = _$q_;
        $rootScope = _$rootScope_;
        deffered = $q.defer();
        myServiceMock = {
            'get': jasmine.createSpy('myService.get').and.returnValue(deferred.promise)
        };

        spyOn($ionicPlatform, 'ready').and.callThrough();

        controller = $controller('myController', {
            '$ionicPlatform': $ionicPlatform,
            'myService': myServiceMock
        });
    }));

    it('should call myService.get after $ionicPlatform.ready is fired', function() {
        expect($ionicPlatform.ready).toHaveBeenCalled(); 
        // ---> [OK]

        expect(myServiceMock.get).toHaveBeenCalled();
        // ---> [ERROR] Expected spy myService.get to have been called
    });
}

我希望我已经清楚了,有人能够帮助我。我找不到任何关于此的好文件。

最好的问候。

0 个答案:

没有答案