Ember-CLI 测试:请在适配器上设置 `firebase` 属性

Ember-CLI tests: Please set the `firebase` property on the adapter

我有一个带有 Ember-CLI 并使用 Firebase 的 Ember.js 应用程序。当我 运行 /tests 时,我得到的唯一错误如下:

Error: Please set the firebase property on the adapter. at init (http://localhost:4200/assets/vendor.js:99854:15) (...etc.)

我的 application.js 适配器代码是 emberfire 安装后的标准代码:

import Ember from 'ember';
import FirebaseAdapter from 'emberfire/adapters/firebase';

const { inject } = Ember;

export default FirebaseAdapter.extend({
  firebase: inject.service(),
});

我的 firebase:URL 设置在 environment.js 文件中。任何人都可以指出问题所在的方向吗?应用程序本身 运行 没问题。我意识到这是 emberfire 的 init 函数中内置的错误响应,但这对我没有帮助!我敢肯定它对入门者来说一定是小而明显的东西,但我仍在学习曲线上...

提前致谢。

Ember 1.13.7 - Ember 数据 1.13.8 - Firebase 2.3.0 - EmberFire 1.5.0 - jQuery 1.11.3

我通过修改 tests/unit/adapters/application-test.js 文件解决了这个问题:

import { moduleFor, test } from 'ember-qunit';
import FirebaseAdapter from 'emberfire/adapters/firebase';

moduleFor('adapter:application', 'Unit | Adapter | application', {
  // Specify the other units that are required for this test.
  // needs: ['serializer:foo']
});

// Replace this with your real tests.
test('it exists', function(assert) {
  var adapter = FirebaseAdapter; //this.subject();
  assert.ok(adapter);
});

希望对其他人有所帮助!

我认为在这种情况下,用 needs 属性 指定缺少的单元更合适,因为适配器无法注入 Firebase 服务。

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

moduleFor('adapter:application', 'Unit | Adapter | application', {
  // Specify the other units that are required for this test.
  needs: ['service:firebase']
});

// Replace this with your real tests.
test('it exists', function(assert) {
  let adapter = this.subject();
  assert.ok(adapter);
});

希望这对您的测试有所帮助。

您还必须注入配置,因为 firebase 服务需要配置。

export default {
  create(application) {
    const config = getOwner(application)._lookupFactory('config:environment');

所以正确答案是:

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

moduleFor('adapter:application', 'Unit | Adapter | application', {
  // Specify the other units that are required for this test.
  needs: ['service:firebase', 'config:environment']
});

// Replace this with your real tests.
test('it exists', function(assert) {
  let adapter = this.subject();
  assert.ok(adapter);
});