If a mixin has an injected service, trying to instantiate an object that uses the mixin will fail with this error when trying to retrieve the service.
Error: Assertion Failed: Attempting to lookup an injected property on an object without a container, ensure that the object was instantiated via a container.
This commit shows the setup, the line that fails is when the service is retrieved in the mixin.
I have the same issue with an object using a mixin. The only difference is the service is injected on to the object and not on the mixin. The Object is instantiated via a container.
@et This error is correct. When you create the object here, you have provided no container. Thus, the injection cannot be made. You will need to pull the object out of the container.
@nullrocket if the factory is instantiated by the container, and the injected factory is also in the container, the injection is expected to work. If it doesn't a reproduction would be great.
I'm going to close this ticket, but please re-open or make a new issue if I've missed something.
What was the actual solution here? I see in the original repo that a needs array was added to the module call, but that doesn't seem to solve my essentially identical problem in 1.13.
@mixonic What exactly should be done to "pull the object out of the container"?
I have this issue opened detailing my case.
@joshforisha I mean using container.lookup('type:name') to fetch an instance instead of using .create. You can also pass the container to create directly and I think that would work for the purposes of your test.
Specifically, here's how you use lookup instead of create:
import Ember from 'ember';
import FooMixin from 'my-app/mixins/foo';
import { moduleFor, test } from 'ember-qunit';
moduleFor('mixin:foo', {
needs: [
'service:bar',
],
});
test('it works', function testItWorks(assert) {
const FooObject = Ember.Object.extend(FooMixin);
this.registry.register('test:subject', FooObject);
const subject = this.container.lookup('test:subject');
assert.ok(subject);
});
@pwfisher Thanks for that. I would note that I scratched my head looking at that code until I realized I needed ember-qunit instead of qunit. http://www.chriskrycho.com/2016/testing-emberjs-mixins-with-a-container.html also helped.
Most helpful comment
Specifically, here's how you use
lookupinstead ofcreate: