如何在Karma单元测试中解析控制器中的$ q.all?

时间:2022-09-25 10:32:46

My controller has:

我的控制器有:

        switchUserAccount: function() {
            $scope.model.currentMode = 'user';
            console.log(ipCookie('currentPatientId'));
            $q.all([facilityCache.getFacility(), facilityGroupCache.getGroupList(), languageCache.getLanguageList(), genderCache.getGenderList(), raceCache.getRaceList(), dosingCache.getDosingOptions()])
                .then(function(){
                    console.log('back from then');
                    cache.set('ui', 'adminPage', '');
                    cache.set('ui', 'schedulePage', 'patients');
                    if(ipCookie('currentPatientId')) {
                        $location.path('/patient/view/' + ipCookie('currentPatientId'));
                    } else {
                        $location.path('/patients');
                    }
                });
        },

and my test is

我的考试是

describe('MainHeaderController', function() {
    var scope, $rootScope, $locationMock, $httpBackend;

    beforeEach(function() {
      module('mapApp');

      return inject(function($injector) {
        var $controller, $q, ipCookieMock;
        $rootScope = $injector.get('$rootScope');
        $controller = $injector.get('$controller');
        $httpBackend = $injector.get('$httpBackend');
        $q = $injector.get('$q');

        $locationMock = jasmine.createSpyObj('$location', ['path'])
        ipCookieMock = function() {
            return 123;
        }

        scope = $rootScope.$new()

        $controller('MainHeaderController', {
            $scope: scope,
            $location: $locationMock,
            $q: $q,
            ipCookie: ipCookieMock
        });

        $httpBackend.whenGET('/rpc/session').respond(200);
        $httpBackend.whenPOST('/rpc').respond(200);
        return scope.$digest();

      });
    });


    it('should redirect to a patient view if a cookie is set', function($rootScope) {
        scope.switchUserAccount();

        // $rootScope.$apply();
        expect($locationMock.path).toHaveBeenCalledWith('/patient/view/123');
    });

});

So what I expect to happen is for $location.path to be called with /patient/view/123. Instead, what I get is

所以我期望发生的是使用/ patient / view / 123调用$ location.path。相反,我得到的是

Expected spy $location.path to have been called with [ '/patient/view/123' ] but actual calls were [  ].

If I uncomment out the $rootScope.$apply(), I get

如果我取消注释$ rootScope。$ apply(),我得到

TypeError: 'undefined' is not a function (evaluating '$rootScope.$apply()')

So how can I trigged the $q.all in my controller so that the test can pass properly?

那么如何在我的控制器中触发$ q.all以便测试可以正常通过?

Thanks!

1 个解决方案

#1


You're hiding the $rootScope variable of your test suite by declaring it as an argument of your test function. That's why it's undefined: jasmine calls the test functions withput any argument.

您通过将其声明为测试函数的参数来隐藏测试套件的$ rootScope变量。这就是为什么它是未定义的:jasmine调用测试函数输出任何参数。

Replace

it('should redirect to a patient view if a cookie is set', function($rootScope) {

by

it('should redirect to a patient view if a cookie is set', function() {

#1


You're hiding the $rootScope variable of your test suite by declaring it as an argument of your test function. That's why it's undefined: jasmine calls the test functions withput any argument.

您通过将其声明为测试函数的参数来隐藏测试套件的$ rootScope变量。这就是为什么它是未定义的:jasmine调用测试函数输出任何参数。

Replace

it('should redirect to a patient view if a cookie is set', function($rootScope) {

by

it('should redirect to a patient view if a cookie is set', function() {