mardi 26 juillet 2016

Ember Addon: writing unit tests for files in my addon folder

I am writing an Ember Addon that provides some services that are not exposed through the app/ folder.

// project Foo
// addon/services/foo.js
import Ember from 'ember';
export default Ember.Service.extend({})

The unit test that gets generated by Ember CLI uses moduleFor helper.

// tests/unit/services/foo.js
import { moduleFor, test } from 'ember-qunit';

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

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

Problem is that since my FooService is not exposed through the app/ folder, moduleFor helper cannot find it using service:foo name.

What would be the best way to unit test my service here? I can see three possibilities: 1) add tests/dummy/app/services/foo.js that exports FooService

// tests/dummy/app/services/foo.js
export { default } from 'foo/services/foo.js';

2) create initializers in the dummy app that registers service:foo

// tests/dummy/app/initializers/account-data.js
import FooService from 'foo/services/foo'
export function initialize(application) {
  application.register('service:foo', FooService);
}

3) don't use moduleFor helper.




Aucun commentaire:

Enregistrer un commentaire