Ember-auth QUnit tests fail every second run

2019-07-07 03:09发布

问题:

I have recently managed to get my ember-auth tests to pass, but it's not yet perfect, as the tests fail every second time (or if there are several tests running, every second test about authentication fails).

I have created a small sample app to demonstrate the failure.

You should be able to reproduce the failure by following these steps:

1. git clone https://github.com/pedrokost/ember_auth_testing_example.git
2. cd ember_auth_testing_example
3. ruby -run -e httpd . -p5000 # (or any HTTP server like: python -m SimpleHTTPServer)
4. Open http://localhost:5000?test in the browser

If the tests passes, refresh the page and see it fail with "Invalid JSON format". Next time you refresh it works again.

回答1:

I had this exact problem when using local storage to save my session. This is because the ember-testing does not clear local storage by default. There are at least three ways to solve this:

  1. Use ephemeral storage with Ember Auth
  2. Add local storage deletion to your setup/tearDown functions
  3. Add Ember Auth logout to the tearDown function

The first solution seems to be the simplest and best solution since it's possible to set the storage to just for Ember Auth. The ember-auth documentation explains storage options:

Cookie

App.Auth = Em.Auth.extend 
   session: 'dummy'

Dummy (for unit tests)

App.Auth = Em.Auth.extend 
   session: 'cookie'

You would want to set yours to Dummy in your initialization if Ember.testing === true