3

I have a simple route in my app like this:

Dash.PostsNewRoute = Em.Route.extend({
  model: function() {
    return this.store.createRecord('post');
  },

  actions: {
    saveForm: function() {
      this.modelFor('postsNew').save();
    }
  }
});

Here is the test I've written to test saveForm and ensure that it's been called:

...
context('create new post', function() {
  beforeEach(function() {
    ...
  });

  it('calls submit on route', function() {
    var mock;
    mock = sinon.mock(testHelper.lookup('route', 'posts.new'));
    mock.expects('actions.saveForm').once();

    this.submitButton.click();

    mock.verify();
    mock.restore();
  });
});

However, I get an error with this implementation: Attempted to wrap undefined property actions.saveForm as function

If I change the route and it's test like this, it'll work:

// Moving the save out of action and call it
Dash.PostsNewRoute = Em.Route.extend({
  model: function() {
    this.store.createRecord('post');
  },

  save: function() {
    this.modelFor('postsNew').save()
  },

  actions: {
    saveForm: function() {
      this.save();
    }
  }
});

The new test:

  it('calls submit on route', function() {
    var mock;
    mock = sinon.mock(testHelper.lookup('route', 'posts.new'));
    mock.expects('save').once();

    this.submitButton.click();

    mock.verify();
    mock.restore();
  });

This way the test will pass. Is it possible to test actions.saveForm directly? Is the limitation of sinon that prevents me from accessing actions.saveForm?

ArashM
  • 1,379
  • 13
  • 18

1 Answers1

3

You could mock the actions hash and set expectation for it like so:

mock = sinon.mock(testHelper.lookup('controller', 'posts_new')._actions);
mock.expects('save').once();

this.submitButton.click();

mock.verify();
mock.restore();
qminhdo
  • 86
  • 6