2016-05-12 91 views
2
我有,在我的單元測試套件的時候我嘗試檢查 因爲getOwner()注入密新服務已加入灰燼彈出一個錯誤的問題

(棄用指南here)。注射服務爲一體的混入Ember2.3 +

這是我的mixin:

import Ember from 'ember'; 

export default Ember.Mixin.create({ 
    sha: Ember.inject.service('sha512'), 
}); 

這是我的基本單元測試小幅生成後改變燼-CLI:

import Ember from 'ember'; 
import DirtyRelationshipsDetectorMixin from 'xamoom-customer/mixins/dirty-relationships-detector'; 
import { module, test } from 'qunit'; 

module('Unit | Mixin | dirty relationships detector'); 

test('it works', function(assert) { 
    let DirtyRelationshipsDetectorObject = Ember.Object.extend(DirtyRelationshipsDetectorMixin); 
    let subject = DirtyRelationshipsDetectorObject.create(); 
    assert.ok(subject); 
    assert.ok(subject.get('sha')); // problem occurs here 
}); 

我得到的錯誤信息是很清楚,但我還沒有找到解決方案:

Error: Assertion Failed: Attempting to lookup an injected property on an object without a container, ensure that the object was instantiated via a container.

服務在那裏,當應用程序正在運行,它只是測試失敗。 燼2.5.1 - 燼-CLI 2.5.0

回答

1

如果使用Ember.getOwner(target)你不能只是.create()目標,但inject the owner..create(owner.ownerInjection())。通常,所有者是應用程序實例。

編輯:

您在實際使用getOwner當你使用Ember.inject。它就像這樣的快捷方式:

sha: Ember.computed({ 
    get() { 
    return Ember.getOwner(this).lookup('service:sha'); 
    } 
}) 
+0

謝謝你的幫助。正如你所看到的,我根本不使用Ember.getOwner()。 我試着將create()更改爲:let subject = DirtyRelationshipsDetectorObject.create – Pavol

+1

我編輯了答案。它是一個單元測試,所以如果你使用這個服務,你會怎麼看? – Lux

+0

好吧,當你解釋這個快捷方式的時候,現在很有意義。我是否明白 - 我不應該明確測試這個屬性,而是模擬它(後來在我的測試套裝中)呢? – Pavol