i am writting unit tests for my controller. I am testing the userSave function with jasmine and want to check the contents of the $scope.alerts variable which is an, but $scope.alerts in empty when it gets out of the userSave function,so i cant check for its content. I dont know why its empty since $scope.alerts is glabl. Below is the code for my controller and the test case.
controller
'use strict';
angular.module('myApp')
.controller('RegisterCtrl', function ($scope, $http, $location) {
$scope.message = 'Register Route';
$scope.init = function () {
$scope.inviteCode = $location.hash();
};
$scope.init();
$scope.master = {};
// Array to hold alert messages
$scope.alerts = [];
$scope.closeAlert = function (index) {
$scope.alerts.splice(index, 1);
};
$scope.userSave = function (user) {
/*jshint unused: false */
$http.post('api/users/v1/', {
'invite_code': $scope.inviteCode,
'password': user.password,
'last_name': user.lastName,
'first_name': user.firstName
}).success(function (data, status, headers, config) {
$location.url($location.path());
$location.path('/');
}).error(function (data, status, headers, config) {
if (status === 404) {
$scope.alerts.push({
type: 'danger',
msg: 'Your invitation code has expired or is invalid, your registration will require a new one to be completed'
});
} else if (status === 204) {
$scope.alerts.push({
type: 'danger',
msg: 'This email already exists, please click login and forgot your password to recover your password'
});
} else {
$scope.alerts.push({
type: 'danger',
msg: 'There was an error registering'
});
}
});
};
});
Test case
'use strict';
describe('Controller: RegisterCtrl', function () {
// load the controller's module
beforeEach(module('myApp'));
var RegisterCtrl, scope, httpBackend,http;
// Initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope, $httpBackend,$http) {
scope = $rootScope.$new();
httpBackend = $httpBackend;
http = $http;
RegisterCtrl = $controller('RegisterCtrl', {
$scope: scope
});
}));
afterEach(function() {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
it('should return a 404 for expired inviteCode', function () {
var userClientPayload = {
lastName: 'pav',
firstName: 'nga',
password: 'test'
};
var userServerPayload = {
invite_code: 'expiredCode',
last_name: 'pav',
first_name: 'nga',
password: 'test'
};
httpBackend.whenPOST('api/users/v1/',userServerPayload).respond(404);
scope.inviteCode = 'expiredCode';
scope.userSave(userClientPayload);
//httpBackend.expectPOST('api/users/v1/',userServerPayload).respond(404);
console.log(scope.alerts);
expect(scope.alerts.type).toBe('danger');
expect(scope.alerts.msg).toBe('Your invitation code has expired or is invalid, your registration will require a new one to be completed');
httpBackend.flush();
});
});
Error
Controller: RegisterCtrl should return a 404 for expired inviteCode FAILED
Expected undefined to be 'danger'.
at /Users/z001hm0/Documents/api_portal/developer-portal/client/test/unit/controllers/register.controller.spec.js:59
Expected undefined to be 'Your invitation code has expired or is invalid, your registration will require a new one to be completed'.
at /Users/z001hm0/Documents/api_portal/developer-portal/client/test/unit/controllers/register.controller.spec.js:60
Aucun commentaire:
Enregistrer un commentaire