Testing is a core part of the Ember framework and its development cycle.
Test framework
How to run
Under js_app/frontend/
folder:
ember test
: run testsember test --test-page='tests/index.html?coverage=true'
: run tests with coveragerake coverage:js
: runt tests with coverageember test -filter 'test des'
: run one test what des is ‘test des’ember test --server
: run tests and e-run tests on every file-changeember server; localhost:4200/test
: run tests on server
Test helpers
moduleFor(fullName [, description [, callbacks]])
fullName
: (String) - The full name of the unit, iecontroller:application
,route:index
.description
: (String) optional - The description of the modulecallbacks
: (Object) optional- QUnit callbacks (
beforeEach
andafterEach
) - ember-test-helpers callback (
subject
) integration: true
orunit: true
(default:integration: true
)needs
specify any dependencies the tested module will require.
- QUnit callbacks (
moduleForComponent(name, [description, callbacks])
name
: (String) - the short name of the component that you’d use in a
template, iex-foo
,ic-tabs
, etc.description
: (String) optional - The description of the modulecallbacks
: (Object) optional- QUnit callbacks (
beforeEach
andafterEach
) - ember-test-helpers callback (
subject
) integration: true
orunit: true
(default:integration: true
)needs
specify any dependencies the tested module will require. (Including this will force your test into unit mode).
- QUnit callbacks (
moduleForModel(name, [description, callbacks])
name
: (String) - the short name of the model you’d use instore
operations ieuser
,assignmentGroup
, etc.description
: (String) optional - The description of the modulecallbacks
: (Object) optional- QUnit callbacks (
beforeEach
andafterEach
) - ember-test-helpers callback (
subject
) integration: true
orunit: true
(default:integration: true
)needs
specify any dependencies the tested module will require.
- QUnit callbacks (
Test Model
1 | import { test, moduleForModel } from 'ember-qunit'; |
Test controllers
If there is a controller:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16export default Ember.Controller.extend({
propA: 'You need to write tests',
propB: 'And write one for me too',
setPropB: function(str) {
this.set('propB', str);
},
actions: {
setProps: function(str) {
this.set('propA', 'Testing is cool');
this.setPropB(str);
}
}
});
Test code1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21test('calling the action setProps updates props A and B', function(assert) {
assert.expect(4);
// get the controller instance
var ctrl = this.subject();
// check the properties before the action is triggered
assert.equal(ctrl.get('propA'), 'You need to write tests');
assert.equal(ctrl.get('propB'), 'And write one for me too');
// trigger the action on the controller by using the `send` method,
// passing in any params that our action may be expecting
Ember.run(()={
ctrl.send('setProps', 'Testing Rocks!');
});
// finally we assert that our values have been updated
// by triggering our action.
assert.equal(ctrl.get('propA'), 'Testing is cool');
assert.equal(ctrl.get('propB'), 'Testing Rocks!');
});
1 | import { test, moduleForComponent } from 'ember-qunit'; |
1 | test('sometimes async gets rejected', function(assert) { |