How do we unit test Mixin model with ember-cli

I have a little Ember.Mixin in my application that contains DS.attr() and / or DS.belongsTo() . I was wondering how to do unit test?

By default, ember-cli generates this test.

 test('it works', function(assert) { var MyModelObject = Ember.Object.extend(MyModelMixin); var subject = MyModelObject.create(); assert.ok(subject); }); 

But when I tried to interact with DS.attr() , I got the following error:

 TypeError: Cannot read property '_attributes' of undefined at hasValue (http://localhost:4200/assets/vendor.js:90650:25) at Class.get (http://localhost:4200/assets/vendor.js:90730:13) at Descriptor.ComputedPropertyPrototype.get (http://localhost:4200/assets/vendor.js:29706:28) at Object.get (http://localhost:4200/assets/vendor.js:35358:19) at Class.get (http://localhost:4200/assets/vendor.js:49734:38) at Object.<anonymous> (http://localhost:4200/assets/tests.js:20126:25) at runTest (http://localhost:4200/assets/test-support.js:2779:28) at Object.run (http://localhost:4200/assets/test-support.js:2764:4) at http://localhost:4200/assets/test-support.js:2906:11 at process (http://localhost:4200/assets/test-support.js:2565:24) 

What makes feelings. What is the best way to do this? Should I create a DS.Model in the test and then apply mixing to it?

Thanks!

+5
source share
1 answer

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); }); }); 
+4
source

All Articles