-->

jasmine test fails with undefined is not a functio

2020-02-24 11:54发布

问题:

I have a following controller:

.controller('ProjectUserAddCtrl', ['$scope', 'ProjectUser', '$q', 'i18nNotifications',     
function($scope, ProjectUser, $q, i18nNotifications) {
    var buildUnassignedUsers = function(users, project) {
        var unassignedUsers = [];
        angular.forEach(users, function(user) {
            var match;
            angular.forEach(project.projectUsers, function(projectUser) {
                if(match) {return;}
                if(projectUser.user.id === user.id) {
                    match = true;
                }
            });

            if(!match) {
                unassignedUsers.push(user);
            }
        });

        $scope.unassignedUsers = unassignedUsers;
    };     

    $q.all([
            $scope.users,
            $scope.project
    ]).then(function(result) {
            buildUnassignedUsers($scope.users, $scope.project);
            $scope.$watch('project', function(newVal) { 
                buildUnassignedUsers($scope.users, $scope.project); }, true
            );
    });
}]);

And a following test in jasmine:

describe('ProjectUserAddCtrl', function() {
    var ctrl;
    beforeEach(function(){
        $scope.users = [];
        $scope.project = {
            projectUsers: []
        };
        ctrl = $controller('ProjectUserAddCtrl', {$scope:$scope, ProjectUser:ProjectUser, $q:$q, i18nNotifications:i18nNotifications});
    });

    it('should create a new instance', function() {
        expect(ctrl).toBeDefined();
    });

    // this test fails!
    it('should create a list of unassigned users', function() {
        $scope.$apply(); // need to call apply to resolve promises
        expect($scope.unassignedUsers).toBeDefined();
    });

});

'should create a list of unassigned users' test fails with this error:

TypeError: 'undefined' is not a function(evaluating $browser.$$checkUrlChange())

I really have no idea why. Any help appreciated.

回答1:

It seems this issue happens when you have mismatch between angular.js and angular-mocks.js Make sure the two files are of the same version.

Please ignore my original comment to the question



回答2:

I had experienced exactly the same issues with our rails project.

We upgraded angular.js to 1.2.24, and then our teaspoon testsuite started failing. I looked into angular.js sources/commits story etc., and then realized, that we had forgot to update angular mocks (we were using old 1.2.20 version, so we need to run bundle update rails-assets-angular-mocks to force this change). After applying new mocks (they already have $$checkUrlChange function mock) everything started working.

So It looks like you also try to use old mocks objects.