# Unit Testing Ember Data Models

**URL:** <https://discuss.emberjs.com/t/unit-testing-ember-data-models/3130>\
**Category:** Ember Data\
**Created:** [October 26, 2013, 5:53pm UTC](https://discuss.emberjs.com/t/unit-testing-ember-data-models/3130 "2013-10-26T17:53:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgwhite](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jgwhite/32/3678_2.png) [@jgwhite](https://discuss.emberjs.com/u/jgwhite)\
**Post date:** [October 26, 2013, 5:53pm UTC](https://discuss.emberjs.com/t/unit-testing-ember-data-models/3130/1 "2013-10-26T17:53:38Z")

</div>

Unit testing Ember Data models seems to involve a bit of boilerplate right now. For example, a simple test for a computed property ends up looking like this:

```javascript
module('User');

test('displayName', function() {
  var store = App. __container__.lookup('store:main'),
      model;

  Ember.run(function() {
    model = store.createRecord('user', {
      id: 1,
      firstName: 'Tom',
      lastName: 'Dale'
    });
  });

  equal(model.get('displayName'), 'Tom Dale');
});

```

So in this case we’re reaching into the ` __container__ ` cookie jar. Another trick to get things going would be to use another supposedly private piece of the system, `_create`:

```javascript
module('User');

test('displayName', function() {
  var model = App.User._create({
    firstName: 'Tom',
    lastName: 'Dale'
  });

  equal(model.get('displayName'), 'Tom Dale');
});

```

Neither of these approaches seem ideal.

Anyone got any suggestions for going about this? And, perhaps more importantly, explaining it to newcomers to the framework?

---

<div class="post-metadata">

**Author:** ![Floby](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/floby/32/14796_2.png) [@Floby](https://discuss.emberjs.com/u/Floby)\
**Post date:** [October 28, 2013, 1:19pm UTC](https://discuss.emberjs.com/t/unit-testing-ember-data-models/3130/2 "2013-10-28T13:19:58Z")

</div>

For most of my computed properties and specific behaviour on models, I moved the funcitonnality to a Mixin like this.

```javascript
App.UserMixin = Ember.Mixin.create({
  fullName: function () {
    return this.get('firstName') + ' ' + this.get('lastName');
  }.property('firstName', 'lastName')
});
  
App.User = DS.Model.extend(App.UserMixin, {
  firstName: DS.attr('string'),
  lastName: DS.attr('string')
});
  
// -------------------
  
describe('User', function () {
  var User = Ember.Object.extend(App.UserMixin);
  it('concat the first name and the last name into the full name', function () {
    var user = User.create({
      firstName: 'Tom',
      lastName: 'Dale'
    });
    expect(user.get('lastName')).to.equal('Tom Dale');
  });
})

```

This tests only your computed property and ensures your record behave correctly. This is not ideal for ember-data specific custom behaviours but it works for the most part.

---

<div class="post-metadata">

**Author:** ![jgwhite](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jgwhite/32/3678_2.png) [@jgwhite](https://discuss.emberjs.com/u/jgwhite)\
**Post date:** [October 28, 2013, 5:57pm UTC](https://discuss.emberjs.com/t/unit-testing-ember-data-models/3130/3 "2013-10-28T17:57:44Z")

</div>

@Floby that’s a nice hack-free solution.

Here’s another decoupled approach:

```javascript
module('User');

test('displayName', function() {
  var user = new App.User();

  user.setProperties({
    firstName: 'Tom',
    lastName: 'Dale'
  });

  equal(user.get('displayName'), 'Tom Dale');
});

```

**UPDATE**

This approach leads to unfortunate `Attempted to handle event didSetProperty ... while in state root.empty` errors ☹

Looks like the mixin approach is probably the safest for disentangled unit tests.

---

<div class="post-metadata">

**Author:** ![samg](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/samg/32/15206_2.png) [@samg](https://discuss.emberjs.com/u/samg)\
**Post date:** [October 28, 2013, 6:26pm UTC](https://discuss.emberjs.com/t/unit-testing-ember-data-models/3130/4 "2013-10-28T18:26:32Z")

</div>

If you look at Ember Data’s own tests, you can find a handy function in tests/ember\_configuration.js called `setupStore`. This function sets up a new “Container” and registers the necessary bits: a store, adapters, serializers, and model definitions.

This method is used in the Ember Data unit tests, so you can see how it is used.

I was pretty amazed when I saw how integrated the unit tests were, but now I see that isolation is something that still needs a lot of work and we’ll probably be stuck with highly integrated tests for a long while.

---

<div class="post-metadata">

**Author:** ![jgwhite](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jgwhite/32/3678_2.png) [@jgwhite](https://discuss.emberjs.com/u/jgwhite)\
**Post date:** [October 28, 2013, 7:11pm UTC](https://discuss.emberjs.com/t/unit-testing-ember-data-models/3130/5 "2013-10-28T19:11:07Z")

</div>

Agreed.

I guess the question is: what would we like these tests to look like? Or, moreover, what would a new user expect?

Probably this:

```javascript
module('User');

test('displayName', function() {
  var user = App.User.create({
    firstName: 'Tom',
    lastName: 'Dale'
  });

  equal(user.get('displayName'), 'Tom Dale');
});

```

But is this achievable without sacrificing robustness and functionality?

---

<div class="post-metadata">

**Author:** ![thenickcox](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/thenickcox/32/15675_2.png) [@thenickcox](https://discuss.emberjs.com/u/thenickcox)\
**Post date:** [April 8, 2014, 2:56pm UTC](https://discuss.emberjs.com/t/unit-testing-ember-data-models/3130/6 "2014-04-08T14:56:12Z")

</div>

I think I can speak to this as a newcomer to the framework, @jgwhite. What you suggested someone new to the framework was right on. And although it is disappointing to have to open up the `App. __container__ ` business, I was struggling to find an example of testing an Ember Data-backed model, and your example helped a lot. Thanks.

---

<div class="post-metadata">

**Author:** ![jgwhite](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/jgwhite/32/3678_2.png) [@jgwhite](https://discuss.emberjs.com/u/jgwhite)\
**Post date:** [April 9, 2014, 6:28am UTC](https://discuss.emberjs.com/t/unit-testing-ember-data-models/3130/7 "2014-04-09T06:28:35Z")

</div>

@thenickcox glad to hear that example helped.

The [new testing guides](https://github.com/emberjs/website/pull/1401) from @cavneb and co are due to land really soon. They’ll become the go-to resources for this topic.
