1
votes

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);
}

EDIT turns out I can't do this. it's not that moduleFor helper is trying to find 'service:foo' but trying to register 'app/services/foo' as 'service:foo'. So registering 'service:foo' ahead of the time does not help.

3) don't use moduleFor helper.

EDIT By this I meant something like what @Andy Pye's answer. But it would be nice to be able to use moduleFor helper... Especially for models since I need access to store.

EDIT turns out it gets more complicated for models since I can't create them directly. So if I go with not using moduleFor helper, I need an instead of store object...

1
actually turns out I cannot do 2)... i guess moduleFor helper is trying to look up the module and register based on the name and it can't find it.lordofthefobs

1 Answers

1
votes

I've just had the same (or a very similar) issue. I've found that this seems to work:

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

module('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 = FooService.create();
    assert.ok(service);
});