How should controller actions be unit tested?

A controller action is passed to components as an action closure. The action returns a promise.

The guides demonstrate that actions can be tested via controller.send but that only triggers the action and does not return a value.

How should a controller action be unit tested?

Is unit testing the right approach?

You should be able to invoke the fn via subject.actions.save.call(subject, undefined);. Since closure action’s don’t bubble, that’s probably what you’re after.

If that doesn’t work, there is always this approach:

export default Ember.Controller.extend({
  actions: {
    save() {
      return this.handleSave();
    }
  },
  handleSave() {
    // unit test this
  }
});
1 Like