2017-08-08 82 views
1

我有一个使用this.get('model.property')的组件,它可以按预期工作。Ember.js + Mirage:在集成测试中拉扯嘲弄的关系

对于我使用的是幻影,这已为我所有的其他测试工作(集成测试包括)我的集成测试,但是当我测试这个特定组件,我得到:

TypeError: Cannot read property 'then' of undefined

这是什么我的测试看起来像:

import { moduleForComponent, test } from 'ember-qunit' 
import hbs from 'htmlbars-inline-precompile' 
import { startMirage } from 'app/initializers/ember-cli-mirage' 
import Ember from 'ember' 

moduleForComponent('summary-card', 'Integration | Component | summary card', { 
    integration: true, 
    beforeEach() { 
    this.server = startMirage() 
    }, 
    afterEach() { 
    this.server.shutdown() 
    } 
}) 

test('it renders', function(assert) { 
    const customer = this.server.create('customer') 
    const location = this.server.create('location', { customer }) 
    const manufacturer = this.server.create('manufacturer') 
    const model = this.server.create('device-model', { manufacturer }) 
    this.server.createList('device', 5, { model, customer, location }) 

    const loc = Ember.Object.create(location) 
    this.set('model', loc) 
    this.render(hbs`{{summary-card model=model model-name=model.name tag='location' belongs-to='customer' has-many='device'}}`); 

    assert.equal(this.$('h1').text().trim(), 'Location 0', 'should be titled Location 0') 

}); 

,基本上,我summary-card.js有这样的事情:

this.get('model.' + belongs).then(relationship => {...}) 

其中belongs只是调用组件时设置的任何belongs-to的值。

我有点困惑,因为它看起来像我传递给我的测试的模拟模型并不像真正代表模型一样运行ember s(我使用Mirage进行开发以及)。有什么地方可以找到更多关于那里到底发生了什么的吗?

谢谢!


P.S.我也尝试使用尽可能由server.create()提供location对象,我得到一个稍微不同的错误:

TypeError: _this.get(...).then is not a function

回答

1

好,通过阅读this answer,我设法找到了自己的解决方案,它的作品真的好:

import { moduleForComponent, test } from 'ember-qunit' 
import hbs from 'htmlbars-inline-precompile' 
import Ember from 'ember' 

moduleForComponent('summary-card', 'Integration | Component | summary card', { 
    integration: true 
}) 

test('it renders', function(assert) { 
    this.inject.service('store', {as: 'store'}) 
    let location 

    Ember.run(() => { 
    location = this.store.createRecord('location', { 
     id: 0, 
     name: 'Location 0', 
     customer: this.store.createRecord('customer', { 
     id: 1, 
     name: 'Customer 0' 
     }), 
     devices: [this.store.createRecord('device', {id: 1})] 
    }) 
    }) 

    this.set('model', location) 
    this.render(hbs` 
    {{#summary-card model=model model-name=model.name tag='location' belongs-to='customer' has-many='device'}} 
     <div class='test-content'>Test Content</div> 
    {{/summary-card}} 
    `) 

基本上,我都选择了直接使用存储,而不是使用幻影,和它的作品!