javascript 茉莉花测试失败,未定义不是函数(评估 $browser.$$checkUrlChange())

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25785149/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:00:47  来源:igfitidea点击:

jasmine test fails with undefined is not a function(evaluating $browser.$$checkUrlChange())

javascriptangularjsjasminekarma-jasmine

提问by h3ndr1ks

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())

类型错误:'undefined' 不是函数(评估 $browser.$$checkUrlChange())

I really have no idea why. Any help appreciated.

我真的不知道为什么。任何帮助表示赞赏。

回答by Buzzy

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.

当 angular.js 和 angular-mocks.js 不匹配时,似乎会发生此问题 确保两个文件的版本相同。

Please ignore my original comment to the question

请忽略我对问题的原始评论

回答by Vitali Tsevan

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

我在我们的 rails 项目中遇到了完全相同的问题。

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-mocksto force this change). After applying new mocks (they already have $$checkUrlChangefunction mock) everything started working.

我们将 angular.js 升级到 1.2.24,然后我们的茶匙测试套件开始失败。我查看了 angular.js 源/提交故事等,然后意识到我们忘记更新 angular 模拟(我们使用的是旧的 1.2.20 版本,所以我们需要运行bundle update rails-assets-angular-mocks以强制进行此更改)。在应用新的$$checkUrlChange模拟(他们已经有了模拟功能)之后,一切都开始工作了。

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

所以看起来你也尝试使用旧的模拟对象。