在测试ember组件集成测试时对服务进行存根

时间:2015-11-08 02:42:22

标签: ember.js ember-cli ember-testing

我正在查看余烬测试指南,并且当你想要存根服务时它会说要执行以下操作

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

//Stub location service
const locationStub = Ember.Service.extend({
  city: 'New York',
  country: 'USA',
  currentLocation: {
    x: 1234,
    y: 5678
  },

  getCurrentCity() {
    return this.get('city');
  },
  getCurrentCountry() {
    return this.get('country');
  }
});

moduleForComponent('location-indicator', 'Integration | Component | location indicator', {
  integration: true,

  beforeEach: function () {
    this.register('service:location-service', locationStub);
    this.inject.service('location-service', { as: 'location' });
  }
});

问题是我在使用此代码时得到Uncaught TypeError: this.register is not a function。所以我假设在每个this.register('service:location-service', locationStub);导致问题之前。

任何人都知道如何解决这个问题,或者更正确的方法是什么?此代码目前在Ember的文档中。

1 个答案:

答案 0 :(得分:0)

看起来this.register仅出现在更高版本的ember-qunit上,您需要确保运行的是ember-qunit> = 0.4.13。

  

this.register仅出现在ember-qunit> = 0.4.13。

http://discuss.emberjs.com/t/service-injection-in-component-integration-tests/8956/3