【发布时间】:2014-01-31 15:19:04
【问题描述】:
我对单元测试 (C#) 并不陌生,但我对 AngularJS 中的单元测试非常陌生。 我正在尝试测试我的控制器,到目前为止已经能够让几个测试正常工作,但是有些测试被证明是相当困难的。
我有 $scope 方法调用我们的身份验证服务,该服务返回一个承诺。在“then”函数中,我正在检查用户是否确实经过身份验证,并基于此我将调用一个私有函数,该函数将出去并进行其他服务调用。
目前测试失败并出现以下错误:
Expected spy getConfigurationStatuses to have been called.
Error: Expected spy getConfigurationStatuses to have been called.
如果有人能帮我指出正确的方向,我将不胜感激。我将在下面发布代码-感谢您的帮助!
这是我的规范(不起作用的是“如果用户通过身份验证,则应调用特定配置”规范:
describe('EnvironmentCtrl specs', function(){
var $rootScope = null, $scope = null, ctrl = null;
var Authentication = {
getCredentials: function(){ return true; }
};
var Environment = { getConfigurationStatuses: function(){ return true; } };
beforeEach(module('ngRoute'));
beforeEach(module('environment'));
beforeEach(module(function($provide){
$provide.value('SITE_ROOT', '/');
}));
beforeEach(inject(function(_$rootScope_, _$controller_, _$timeout_, _$location_, _$q_, _Authentication_, _Environment_){
$rootScope = _$rootScope_;
$controller = _$controller_;
$timeout = _$timeout_;
$location = _$location_;
$q = _$q_;
Authentication = _Authentication_;
Environment = _Environment_;
spyOn(Authentication, 'getCredentials').andCallThrough();
spyOn(Environment, 'getConfigurationStatuses').andCallThrough();
$rootScope = _$rootScope_;
$scope = $rootScope.$new();
ctrl = $controller('EnvironmentCtrl', {$rootScope: $rootScope,$scope: $scope, $timeout: $timeout,
Eventor: {}, Controller: {}, Environment: Environment,Authentication: Authentication, ErrorService:{} });
}));
describe('When initializing the EnvironmentCtrl', function(){
// this one works fine!
it('should set default values on the scope object', function(){
expect($scope.controllerName).toEqual('EnvironmentCtrl');
expect($scope.environmentStatusType).toEqual('configurations');
expect($scope.configurationsSelected).toBe(true);
expect($scope.isDataLoaded).toBe(false);
});
// this works fine!
it('should make a call to authenticate the user', function(){
$scope.determineViewToDisplay();
expect(Authentication.getCredentials).toHaveBeenCalled();
});
// this one doesn't work!
it('should call specific configurations if user is authenticated', function(){
$scope.determineViewToDisplay();
$rootScope.isUserAuthenticated = true;
expect(Environment.getConfigurationStatuses).toHaveBeenCalled();
});
});
});
这是单元测试中涉及的三个函数:
$scope.determineViewToDisplay = function () {
Authentication.getCredentials().then(function(){
if ($rootScope.isUserAuthenticated === true) {
$scope.isAnonymous = false;
handleAuthenticatedUserView();
} else {
Eventor.publish('event:login', false);
$scope.isAnonymous = true;
handleAnonymousUserView();
}
}, function(err){
ErrorService.handleError(err, null, $scope.controllerName);
});
};
function handleAuthenticatedUserView() {
$scope.configurationStatusTimer = $timeout(function(){
displayConfigurationStatuses(true);
}, 5);
}
function displayConfigurationStatuses(isAuthenticated) {
Environment.getConfigurationStatuses(isAuthenticated).then(function(statuses){
setConfigurationsIconStatus(statuses);
$scope.configurationStatuses = statuses;
$scope.isDataLoaded = true;
amplify.store($rootScope.productCustomerName + '-configurationStatuses', statuses, {expires: 120000});
$rootScope.showLoadingIndicator = false;
}, function(err){
ErrorService.handleError(err, null, $scope.controllerName);
});
}
【问题讨论】:
标签: javascript angularjs unit-testing jasmine