Recommended way of instantiating classes: container or not?

After looking for examples on how to do proper testing for Ember classes, I have found two different approaches when instantiating them. Supposing we have a MyController class to test:

// Direct method:
var myController = App.MyController.create();

// Container method:
var myController = App.__container__.lookup('controller:my');

What’s the difference between them, and what’s the recommended way to go?

The first way creates a new instance of the class no matter what. App.MyController.create() will always return a different instance of the class. The container, on the other hand, only does that if the class in question was registered with the singleton flag set to false. By default, the container will only create a new instance of the class the very first time it’s looked up; all subsequent lookups return the same instance.

So for the most part, you shouldn’t be using the container directly. Just create a new instance, especially if this is only for testing.

1 Like

How do I access the application’s store class inside a unit test without relying on the following code:

store = app.__container__.lookup('store:main');