In my main describe , I have the following:
beforeEach(inject(function(...) { var mockCookieService = { _cookies: {}, get: function(key) { return this._cookies[key]; }, put: function(key, value) { this._cookies[key] = value; } } cookieService = mockCookieService; mainCtrl = $controller('MainCtrl', { ... $cookieStore: cookieService } }
Later I want to check how the controller believes that the cookie already exists, so I will enclose the following description:
describe('If the cookie already exists', function() { beforeEach(function() { cookieService.put('myUUID', 'TEST'); }); it('Should do not retrieve UUID from server', function() { expect(userService.getNewUUID).not.toHaveBeenCalled(); }); });
However, when I make a change to cookieService , it is not saved in the controller being created. Am I taking the wrong approach?
Thanks!
EDIT: Updated test code, and that is how I use $ cookieStore:
var app = angular.module('MyApp', ['UserService', 'ngCookies']); app.controller('MainCtrl', function ($scope, UserService, $cookieStore) { var uuid = $cookieStore.get('myUUID'); if (typeof uuid == 'undefined') { UserService.getNewUUID().$then(function(response) { uuid = response.data.uuid; $cookieStore.put('myUUID', uuid); }); }
});
source share