EmberJS: How to test a controller action with modu

2019-07-16 17:48发布

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条回答
一纸荒年 Trace。
2楼-- · 2019-07-16 18:50

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.

查看更多
登录 后发表回答