Angular.js: Angular mocks no longer mocking browser cookies

Created on 27 Aug 2015  ·  3Comments  ·  Source: angular/angular.js

We have recently migrated to angular 1.4, and we noticed some tests are intermittently failing due to state that is being leftover between tests in the cookies. It appears the the mocking of cookies was removed from angular-mocks in the latest versions. I have a couple of plunks to illustrate the point.

Here, in angular 1.3, the cookie is set in $cookieStore in one test, and it is not defined in $cookieStore in subsequent tests:
http://plnkr.co/edit/WbRKACxBvJkHUmJwntN4?p=preview

In angular 1.4, however, the cookie is still set on subsequent tests, causing it to fail:
http://plnkr.co/edit/c6jpOmmSuufSYvQysdTQ?p=preview

Was this functionality intentionally removed, and if so can you let me know what the intended workaround is?

ngCookies investigation

Most helpful comment

Does the Angular team believe that no one needs to test setting and getting cookies with ngCookies? I'm shocked to discover that a year after the release of 1.4 this still hasn't been dealt with.

All 3 comments

@shahata do you by any chance know why there's a difference?

@Narretz In previous versions of Angular, $browser.cookies() was completely mocked out by ngMock and writing a cookie through $cookies wrote to an in-memory map and not the browser. We've discussed this in the past and considered adding a test-kit that hooks into new cookies mechanism and gives tools that verify that cookies are written with correct params (it is no longer just about cookie value), but I don't think this is very critical.

@butchpeters I think the best way to go is for you to mock $cookies in your test:

module({$cookies: {
  store:{},
  put: function (key, value) { this.store[key] = value; },
  get: function (key) { return this.store[key]; }
}});

Here's the fixed plunker: http://plnkr.co/edit/LTQHDtDM5FOiTiXl2waT?p=preview

Does the Angular team believe that no one needs to test setting and getting cookies with ngCookies? I'm shocked to discover that a year after the release of 1.4 this still hasn't been dealt with.

Was this page helpful?
0 / 5 - 0 ratings