EmberJS: How to test a controller action with moduleFor of ember-qunit, which uses store of ember-data

五迷三道 提交于 2019-12-11 03:22:20

问题


I want to test a controller action like this:

createNewBase: function () {

  var attributesForNewBase = this.get( 'model' ).getProperties( ... ),
      self = this,
      newBase = this.store.createRecord( ..., {

        ...

      } );

  newBase.save().then( function ( createdBase ) {

    self.send( 'setBaseOfModel', createdBase );

  }, function ( error ) {

    console.log( error );

  } );

}

The problem is that if I use moduleFor of ember-qunit to test this action the store is undefined. So what do I have to do or what is the correct way to create such tests?


回答1:


You could create a mock store. Something like this:

controller.set('store', {
    createRecord: function() {
        return {
            save: function() {
                return Ember.RSVP.resolve();
            }
        };
    }
});

This will allow your controller to function as if the store was really there, while at the same time, alerting you if the controller does anything with the store that you didn't plan for ahead of time.

The alternative would be to actually set up your store for testing, but that's slightly more involved. If you wanted to do that, it might be easier just to write an integration test instead of a unit test.



来源:https://stackoverflow.com/questions/25547701/emberjs-how-to-test-a-controller-action-with-modulefor-of-ember-qunit-which-us

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!