I am new to jasmine and wanted to create a test for the following below, I created the code in the test section, but I get "TypeError: it is not possible to set the" username "property from undefined".
I created the global "cp" namespace in apps.js and used this in the service and controller.
//controller cp.controller = {}; cp.controller.LoginController = function($scope, $location, $cookies){ $scope.signIn = function(){ $cookies.user = $scope.form.username; user.set($scope.form.username); $location.hash( "home" ); } }; //service cp.serviceFactory = {}; cp.serviceFactory.user = function user( $cookies){ var userName = $cookies.user; return{ set: function(name){ userName = name; }, get: function(){ return userName; } } }; //test script describe('Cameo Controllers', function() { describe('LoginController', function(){ var scope, cookies, ctrl, $httpBackend; beforeEach(module('CameoPaaS')); beforeEach(inject(function(_$httpBackend_, $rootScope, $controller, $cookies) { $httpBackend = _$httpBackend_; // cookies = $cookies.username; scope = $rootScope.$new(); cookies = scope.$cookies; ctrl = $controller(cp.controller.LoginController, {$scope: scope, $cookies: cookies}); })); it('should log the user into the system', function() { expect(scope.username).toBeUndefined(); scope.form.username = 'me'; scope.signIn(); //expect(user).toBe(undefined); }); }); });
Question: how to determine and set the value for $ cookies.username in the test script to get around this error.
source share