I'm attempting to perform integration/acceptance tests for my Ember app. I'm specifically testing user authentication (e.g. – submitting the login form) and protected pages/states that require authenticated users.
General notes about my app:
I have three problems:
From the ember-simple-auth API, it refers to using Ephemeral storage for tests. I have done so, much like this. However, it seems that the sessions is still getting stored in local storage. If I do not perform localStorage.clear()
in each test setup/teardown tests fail because I remain logged in when each test runs after the first.
Am I able to prevent storing the session in local storage between each test when I'm using the Devise authenticator for my app?
If I attempt to log in a user in more than 1 test()
, my tests spin off into an infinite loop. The first test will pass, but when the second test submits the login form the entire test suite stops and reboots.
Integration Test #1
App = null
module('Acceptance - Page #1',
setup: ->
App = startApp()
teardown: ->
Ember.run(App, 'destroy')
)
test('page #1 behind authentication', ->
expect(1)
visit('/page-1')
fillIn('input#identification', 'foo@bar.com')
fillIn('input#password', 'password')
click('button[type="submit"]')
andThen(->
equal(true, true) # This test works fine
)
)
Integration Test #2
App = null
module('Acceptance - Page #2',
setup: ->
App = startApp()
teardown: ->
Ember.run(App, 'destroy')
)
test('page #2 behind authentication', ->
expect(1)
visit('/page-2')
fillIn('input#identification', 'foo@bar.com')
fillIn('input#password', 'password')
click('button[type="submit"]')
andThen(->
equal(true, true) # Never runs, tests start over, infinite loop begins
)
)
EAK's api-stubs do not seem to be available for Testem, so the "log in" process in these acceptance test when run via the command line/Testem fail.
I attempted setting up sinon.js, but above mentioned issues have prevented me from deciding if it's actually working correctly or not. What is the best way to successfully stub logging in a user with ember-simple-auth? Is it possible to use EAK's api-stubs for Testem?
With the help of @marcoow and a few other SO questions and GitHub issues I've been able to resolve all my problems:
I was using an outdated API option in my code. Updating to use the newer API's storeFactory
option resolved my session localStorage
issue.
# app/initializers/simple-auth.coffee
if Ember.testing == true
options = Ember.merge({ storeFactory: 'session-store:ephemeral' }, options)
This turned out to be related to another library I was loading called fastclick. After updating my index file to only load this library in non-test environments, my form submission/infinite loop problems went away.
// app/index.html
<!-- @if tests=false -->
<script src="/vendor/emberjs-touch/lib/ember-fastclick.js"></script>
<!-- @endif -->
I found other people facing similar issues to me on StackOverflow. Their questions were eventually responded to/resolved which helped me resolve me own issues. In the comments of this GH issue, there is a link to a work-around to this issue. Example here.