Skip to content

Commit 10f792e

Browse files
committed
update(testing): upd. model testing (emberjs/ember.js#15933)
This updates the model testing section according to the new Ember Qunit testing patterns proposed in RFC#232.
1 parent 3c6f26f commit 10f792e

File tree

1 file changed

+30
-28
lines changed

1 file changed

+30
-28
lines changed

source/testing/testing-models.md

+30-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
_Unit testing methods and computed properties follows previous patterns shown
22
in [Unit Testing Basics] because DS.Model extends Ember.Object._
33

4-
[Ember Data] Models can be tested using the `moduleForModel` helper.
4+
[Ember Data] Models can be tested in a module that uses the `setupTest` helper.
55

66
Let's assume we have a `Player` model that has `level` and `levelName`
77
attributes. We want to call `levelUp()` to increment the `level` and assign a
@@ -28,29 +28,30 @@ export default Model.extend({
2828
```
2929

3030
Now let's create a test which will call `levelUp` on the player when they are
31-
level 4 to assert that the `levelName` changes. We will use `moduleForModel`:
31+
level 4 to assert that the `levelName` changes. We will use `module` together with the `setupTest` helper method:
3232

3333
```tests/unit/models/player-test.js
34-
import { moduleForModel, test } from 'ember-qunit';
35-
import { run } from '@ember/runloop';
34+
import { module, test } from 'qunit';
35+
import { setupTest } from 'ember-qunit';
36+
import { run } from "@ember/runloop";
3637

37-
moduleForModel('player', 'Unit | Model | player', {
38+
module('Unit | Model | player', function(hooks) {
3839
// Specify the other units that are required for this test.
39-
needs: []
40-
});
41-
42-
test('should increment level when told to', function(assert) {
43-
// this.subject aliases the createRecord method on the model
44-
const player = this.subject({ level: 4 });
40+
test('should increment level when told to', function(assert) {
41+
const player = run(() => this.owner.lookup('service:store').createRecord('player'));
4542

46-
// wrap asynchronous call in run loop
47-
run(() => player.levelUp());
43+
// wrap asynchronous call in run loop
44+
run(() => player.levelUp());
4845

49-
assert.equal(player.get('level'), 5, 'level gets incremented');
50-
assert.equal(player.get('levelName'), 'Professional', 'new level is called professional');
46+
assert.equal(player.get('level'), 5, 'level gets incremented');
47+
assert.equal(player.get('levelName'), 'Professional', 'new level is called professional');
48+
});
5149
});
5250
```
5351

52+
Also note, how both creating a record and updating properties on the record through the `levelUp` method requires
53+
us to wrap these operations into a `run` function. You can read more the Ember run loop [over here](https://guides.emberjs.com/v2.18.0/applications/run-loop/).
54+
5455
## Testing Relationships
5556

5657
For relationships you probably only want to test that the relationship
@@ -77,24 +78,25 @@ export default Model.extend({
7778
});
7879
```
7980

80-
Then you could test that the relationship is wired up correctly
81-
with this test.
81+
Then you could test that the relationship by looking it up on the `user` model which it is part of.
8282

8383
```tests/unit/models/user-test.js
84-
import { moduleForModel, test } from 'ember-qunit';
85-
import { get } from '@ember/object';
84+
import { module, test } from 'qunit';
85+
import { setupTest } from 'ember-qunit';
86+
import { get } from "@ember/object"
8687

87-
moduleForModel('user', 'Unit | Model | user', {
88-
// Specify the other units that are required for this test.
89-
needs: ['model:profile']
90-
});
88+
module('Unit | Model | user', function(hooks) {
89+
setupTest(hooks);
90+
91+
test('should own a profile', function(assert) {
92+
const User = this.owner.lookup('service:store').modelFor('user');
9193

92-
test('should own a profile', function(assert) {
93-
const User = this.store().modelFor('user');
94-
const relationship = get(User, 'relationshipsByName').get('profile');
94+
// lookup the relationship on the user model
95+
const relationship = get(User, 'relationshipsByName').get('profile');
9596

96-
assert.equal(relationship.key, 'profile', 'has relationship with profile');
97-
assert.equal(relationship.kind, 'belongsTo', 'kind of relationship is belongsTo');
97+
assert.equal(relationship.key, 'profile', 'has relationship with profile');
98+
assert.equal(relationship.kind, 'belongsTo', 'kind of relationship is belongsTo');
99+
});
98100
});
99101
```
100102

0 commit comments

Comments
 (0)