When I started creating an app with ember I didn't think about testing it. But as the app grew steadily the pains for making mistakes got obvious. So I thought: let's try to begin testing. I had some basic unit tests in place. But that was not enough. Because the most basic things as the possibility to login failed to work.
So I tried to create some acceptance tests. This describes the journey of new knowledge I gained doing this. I assume you have your app in place and are using ember-cli
First the basic command to create the boilerplate for a test in ember-cli ember g acceptance-test login
. This will create a new file inside tests/acceptance/
named login-test.js
. with the following content
import Ember from 'ember';
import { module, test } from 'qunit';
import startApp from 'yourapp/tests/helpers/start-app';
var application;
module('Acceptance | asd', {
beforeEach: function() {
application = startApp();
},
afterEach: function() {
Ember.run(application, 'destroy');
}
});
test('visiting /login', function(assert) {
visit('/login');
andThen(function() {
assert.equal(currentURL(), '/login');
});
});
Nothing special here. ember-cli tries to open the url /login
and assert whether it is on this page.
In my app the login is located directly at the index page so I changed it to visit('/');
Ember has some nice test helpers. So one could use fillIn
to enter username/email and password and click
to send the data to the server through a action.
The middle part of the test then would look like this:
test('visiting / and trying to login', function(assert) {
visit('/');
fillIn('.header .login-email', 'mytest@email.com');
fillIn('.header .login-pw', 'testPW');
click('.header .login-btn');
andThen(function() {
//this should run after being loggedIn
assert.equal(currentURL(), '/');
});
});
But I got the error
Assertion failed: You have turned on testing mode, which disabled the run-loop's autorun. You will need to wrap any code with asynchronous side-effects in an Ember.run
How to fix that will be in the next part of this series of learning to test ember. The next part will be released in 3-4 days.