5
votes

It appears that store is not available in my Ember tests, whether in the context of an ObjectController or within any unit tests. My unit test:

`import {test, moduleFor} from "ember-qunit"`
`import DS from "ember-data"`

moduleFor "controller:register", "RegisterController", {
}

test "store is working", ->
    expect 1
    controller = @subject()
    Ember.run(->
        sampleUser = controller.get("store").createRecord("user", {
            username: "myuser"
            password: "otherpassword"
        })
        ok(sampleUser instanceof DS.Model)
    )

The test will give:

Died on test #1 at test (http://localhost:4200/assets/vendor.js:73539:13) at eval (app/tests/unit/controllers/register-test.js:19:5) at requireModule (http://localhost:4200/assets/vendor.js:54:29) at http://localhost:4200/assets/test-loader.js:14:29: Cannot read property 'createRecord' of null

Can anyone explain why I am not able to access DS capabilities from either within my tests or from within the controller itself (when running tests)?

2
Could you create an instance of the store manually -then inject that /add that to your controller (under test) to verify this works as you'd expect? - Toran Billups

2 Answers

1
votes

Because ember-qunit doesn't inject the store into your controllers, it's meant for unit tests, not integration tests. And Ember Data's store is outside of the scope of that controller.

0
votes

You can add the ember-data store to the "needs" section of the unit test. You will also need to add any models being created to the "needs" section like so:

needs: ['service:store', 'model:user']

This will inject a real instance of the store into the object (controller, component, route, etc.) being tested.