Group testing a mixin model like this is a bit complicated, as accessing the store is required to create a model. As a rule, the store is not available in mixin tests, because there is not even a container. In addition, since we just want to test mixin, we donβt want to require a real model, so we can create and register a fake host model for tests only. Here is how I did it.
First insert "ember-data" and use the helpers from "ember-qunit" instead of the spare helpers from "qunit". Replace this:
import { module, test } from 'qunit';
Wherein:
import { moduleFor, test } from 'ember-qunit'; import DS from 'ember-data';
Then you update your module declaration as follows:
moduleFor('mixin:my-model-mixin', 'Unit | Mixin | my model mixin', { // Everything in this object is available via `this` for every test. subject() { // The scope here is the module, so we have access to the registration stuff. // Define and register our phony host model. let MyModelMixinObject = DS.Model.extend(MyModelMixin); this.register('model:my-model-mixin-object', MyModelMixinObject); // Once our model is registered, we create it via the store in the // usual way and return it. Since createRecord is async, we need // an Ember.run. return Ember.run(() => { let store = Ember.getOwner(this).lookup('service:store'); return store.createRecord('my-model-mixin-object', {}); }); } });
After installing this installation, you can use this.subject() in separate tests to get the object to test.
test('it exists', function(assert) { var subject = this.subject(); assert.ok(subject); });
At the moment, this is just a standard unit test. You may need to wrap the asynchronous or computed code in the Ember.run block:
test('it doubles the value', function(assert) { assert.expect(1); var subject = this.subject(); Ember.run(() => { subject.set('someValue', 20); assert.equal(subject.get('twiceSomeValue'), 40); }); });