在单元测试中使用初始化程序?

And*_*aus 6 unit-testing ember.js ember-cli

我有一个stdlib初始化器.在该初始化程序中,我重新打开一些内置的Ember类并添加一些自定义方法.例如,我添加reverseEachEmber.EnumerableArray.

它们在应用程序中运行良好,但在测试中我收到"reverseEach:undefined不是函数".

我如何表明测试应该使用初始化程序?

我试过了needs: [..., 'initializer:stdlib'].它没有偶然发现,但我仍然收到"未定义"的错误.

这是一个示例测试:

`import { test, moduleForModel } from 'ember-qunit'`

moduleForModel 'foo', 'foo',
  needs: [
    'model:bar'
    'initializer:stdlib'
  ]

test 'deleteLastNItems', ->
  model = @subject()
  model.set '', ['foo', 'bar', 'baz', 'quux']
  model.deleteLastNItems 2 # This should not die with "reverseEach: undefined is not a function"
  deepEquals model.get('someProperty'), ['foo', 'bar']
Run Code Online (Sandbox Code Playgroud)

zhe*_*hua -1

您可以使用ember g initializer stdlib,它会为您生成示例代码,包括测试。

import Ember from 'ember';
import { initialize } from '../../../initializers/stdlib';
import { module, test } from 'qunit';

var registry, application;

module('Unit | Initializer | stdlib', {
  beforeEach: function() {
    Ember.run(function() {
      application = Ember.Application.create();
      registry = application.registry;
      application.deferReadiness();
    });
  }
});

// Replace this with your real tests.
test('it works', function(assert) {
  initialize(registry, application);

  // you would normally confirm the results of the initializer here
  assert.ok(true);
});
Run Code Online (Sandbox Code Playgroud)

参考:Ember 蓝图