简体   繁体   中英

Using ember-qunit to test controllers with a store (DS.FixtureAdapter)

I have an ember-qunit test case for a controller (using moduleFor('controller:name', ...) ) that that I'd like to be able to use the moduleForModel -exclusive this.store() in order to retrieve a DS.FixtureAdapter data store. For this specific test case, I'm not trying to test the model - I just want to verify that the controller can be populated with a set of model instances and various operations can be run against that data.

I'm using coffeescript so my code looks like:

moduleFor("controller:test", 'My Controller', {
  setup: ->
    @store().createRecord 'test', value: 1
    @store().createRecord 'test', value: 2

    @subject({
      model: @store().all('test')
    })
  teardown: -> App.reset()
}, (container, context) ->
  container.register 'store:main', DS.Store
  container.register 'adapter:application', DS.FixtureAdapter
  context.__setup_properties__.store = -> container.lookup('store:main')
)

In the example above there is a controller named TestController and there is also a model named Test. I lifted the container.register and context.__setup_properties__.store lines from the definition of moduleForModel in ember-qunit.

The problem is that I get an error when running the ember-qunit test suite:

Setup failed on [test case name]: No model was found for 'test'

Running the actual application outside of ember-qunit works fine. Maybe there's somebody out there who's had this same issue? Or maybe I'm taking the wrong approach?

Your problem could be that your test model has not been registered in the container, so it cannot be resolved.

You could register manually during your test module callbacks:

container.register('model:test', TestModel) 

Or use the needs property of the moduleFor impl:

moduleForComponent('controller:test', 'My Controller', {

  // specify the other units that are required for this test
  needs: ['model:test'],
  setup: {...},
  teardown: {...}
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM