2016-09-20 7 views
5

DS.attr()および/またはDS.belongsTo()を含むEmber.Mixinはごくわずかです。私はどのように私はそれらをテストする必要がありますかと思っていた?デフォルトではモデルミックスインとember-cliのユニットテストの方法

、燃えさし-CLI

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

このテストを生成しかし、私はDS.attr()と対話しようとしたとき、私は、次のエラーを得た:

感覚を作る
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) 

。それを行う最善の方法は何ですか?テスト中にDS.Modelを作成し、それにmixinを適用する必要がありますか?

ありがとうございます!

答えて

4

このようなモデルミックスインのユニットテストは、モデルを作成するためにストアにアクセスする必要があるため、ややこしいことです。通常、コンテナはないので、店舗はmixinテストでは利用できません。さらに、mixinをテストしたいだけなので、モデルを必要としたくないので、テストのためだけに偽のホストモデルを作成して登録することができます。ここで私はこれをやった。

まず、 'ember-data'をプルし、 'qunit'の在庫ヘルパーの代わりに 'ember-qunit'のヘルパーを使用します。その後、あなたはこのようなあなたのモジュール宣言更新

import { moduleFor, test } from 'ember-qunit'; 
import DS from 'ember-data'; 

:これにより

import { module, test } from 'qunit'; 

:これを交換してください

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', {}); 
    }); 
    } 
}); 

あなたの場所にこの設定をしたら、あなたがして使用することができますがthis.subject()個々のテストでテスト用のオブジェクトを取得します。

test('it exists', function(assert) { 
    var subject = this.subject(); 
    assert.ok(subject); 
}); 

この時点では単なる単体テストです。 Ember.runブロックに非同期または計算コードをラップする必要がある場合があります。

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); 
    }); 
}); 
関連する問題