angular / angular.js

AngularJS - HTML enhanced for web apps!
https://angularjs.org
MIT License
58.8k stars 27.48k forks source link

Angular mocks no longer mocking browser cookies #12692

Open butchpeters opened 9 years ago

butchpeters commented 9 years ago

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?

Narretz commented 9 years ago

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

shahata commented 9 years ago

@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

jough commented 8 years ago

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.