Ember Testing: Unable to read 'createRecord' property from null

It seems that store not available in my Ember tests, whether in the context of an ObjectController or as part of 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:

He died according to test No. 1 when testing ( http://localhost:4200/assets/vendor.js:73539:13 ) on eval (application / tests / block / controllers / register-test.js: 19: 5) in requireModule ( http://localhost:4200/assets/vendor.js:54:29 ) at http://localhost:4200/assets/test-loader.js:14:29 : Unable to read the 'createRecord' property from null

Can someone explain why I can not access the DS capabilities from my tests or from the controller itself (when running tests)?

+5
source share
2 answers

Since ember-qunit does not inject storage into your controllers, this means for unit tests, not integration tests. And the Ember Data repository is outside the scope of this controller.

+1
source

You can add the ember-data datastore to the "needs" unit test section. You also need to add any models created in the "Required" section, for example:

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

This will lead to testing the real storage instance in the test object (controller, component, route, etc.).

0
source

Source: https://habr.com/ru/post/1201983/


All Articles