2

I am having a problem with an error that pops up in my unit test suite when I try to check a service injected into Mixin since getOwner() has been added into Ember (deprecation guide here).

This is my mixin:

import Ember from 'ember';

export default Ember.Mixin.create({
    sha: Ember.inject.service('sha512'),
});

This is my basic unit test slightly changed after being generated by ember-cli:

import Ember from 'ember';
import DirtyRelationshipsDetectorMixin from 'xamoom-customer/mixins/dirty-relationships-detector';
import { module, test } from 'qunit';

module('Unit | Mixin | dirty relationships detector');

test('it works', function(assert) {
  let DirtyRelationshipsDetectorObject = Ember.Object.extend(DirtyRelationshipsDetectorMixin);
  let subject = DirtyRelationshipsDetectorObject.create();
  assert.ok(subject);
  assert.ok(subject.get('sha')); // problem occurs here
});

The error message I am getting is quite clear but I haven't found a solution:

Error: Assertion Failed: Attempting to lookup an injected property on an object without a container, ensure that the object was instantiated via a container.

The service is there when the app is running, it's just the test that fails. Ember 2.5.1 - Ember-CLI 2.5.0

Pavol
  • 1,200
  • 8
  • 20

1 Answers1

2

If you use Ember.getOwner(target) you can not just .create() the target, but inject the owner. with .create(owner.ownerInjection()). Typically an owner is an app instance.

Edit:

You are actually using getOwner when you use Ember.inject. Its like a shortcut for this:

sha: Ember.computed({
  get() {
    return Ember.getOwner(this).lookup('service:sha');
  }
})
Lux
  • 17,835
  • 5
  • 43
  • 73
  • Thank you for your help. I am not using Ember.getOwner() at all as you can see. I tried to change create() to: `let subject = DirtyRelationshipsDetectorObject.create(Ember.getOwner(this).ownerInjection());` but it fails of course since `this` is not an app instance. – Pavol May 12 '16 at 15:48
  • 1
    I edited the Answer. Its a unit test, so what do you think *should* happen if you use the service? – Lux May 12 '16 at 15:52
  • Alright, makes sense now as you explained the shortcut thing. Do I get it well - I should not test this property explicitly and mock it (later in my tests suits) instead? – Pavol May 12 '16 at 16:11
  • 1
    Depends highly on your use case. Probably you mock it, if you don't mock it you need to include the full service, but then you are on integration tests I guess. – Lux May 12 '16 at 16:14