在单元测试中通过Ember CLI Mirage发出加载记录

时间:2016-04-30 11:53:26

标签: ember.js ember-cli-mirage

我收到以下错误Mirage: Your Ember app tried to GET '/statements', but there was no route defined to handle this request. Define a route that matches this path in your mirage/config.js file. Did you forget to add your namespace?

这是在尝试在单元测试中使用海市蜃楼时。我的验收测试工作得很好,似乎可以很好地加载记录。但这可能是因为整个余烬应用程序已启动进行验收测试?

我确定我的配置是正确的,我通过导出testConfig设置它:

export function testConfig() {

  this.urlPrefix = 'http://0.0.0.0:3000'

  this.get('/languages', languages)

  this.get('/statements', statements)

代码目前如下:

//services/fake.js

import Ember from 'ember'

const { Service, A, inject: { service } } = Ember

export default Service.extend({

  store: service(),

  statements: A(),

  loadFakeStatement(){
    this.get('statements').addObject(Ember.Object.create({
      title: 'Statement 1',
      option: 'A'
    }))
  },

  loadRealStatements(){
    this.get('store')
    .findAll('statement')
    .then(statements => {
      this.get('statements').addObjects(statements)
    })
  }

})

// tests / unit / services / fake-test.js

import { moduleFor, test } from 'ember-qunit'
import startMirage from '../../helpers/setup-mirage-for-integration'

moduleFor('service:fake', 'Unit | Service | fake', {

  needs: ['model:statement'],

  beforeEach() {
    startMirage(this.container)
  },

  afterEach() {
    window.server.shutdown()
  }
})

test('initializes correctly', function(assert) {
  const service = this.subject()
  assert.ok(service)
})

test('loads fake data correctly', function(assert) {

  const service = this.subject()

  assert.equal(service.get('statements.length'), 0, 'statements initially 0')

  service.loadFakeStatement()

  assert.equal(service.get('statements.length'), 1, 'statements populated by load')
})

test('loads mirage data correctly', function(assert) {

  const service = this.subject()

  assert.equal(service.get('statements.length'), 0, 'statements initially 0')

  service.loadRealStatements()

  assert.equal(service.get('statements.length'), 1, 'statements populated by load')
})

0 个答案:

没有答案