我有以下控制器:
.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 ); }); }]);
然后在茉莉花中进行以下测试:
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(); }); });
“应创建未分配用户的列表”测试失败,并显示以下错误:
TypeError:’undefined’不是一个函数(评估$ browser。$$ checkUrlChange())
我真的不知道为什么。任何帮助表示赞赏。
当您在angular.js和angular-mocks.js之间不匹配时,似乎会出现此问题。请确保两个文件的版本相同。
请忽略我对问题的原始评论