Jasmine and corners using $ jasmine cookies

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.

+4
source share
2 answers

First of all, make sure you include angular-cookies.js, which were separated from the main distribution in 1.0.0rc3

If it were me, I would include cookie processing in the service and then use jasmine to mock the spy in your cookie-wrapper service implementation. You can find this post . In addition, I found this testing of cookies in block and e2e . IMHO the problem is that it is too close to the metal, and you have to work directly with browsers.

+2
source

I am also facing the same problem, here is a workaround -

 beforeEach(inject(function($cookies){ $cookies.username = 'AngularJs'; })); 

Please suggest if there is a better way.

+2
source

Source: https://habr.com/ru/post/1439430/


All Articles