我的应用程序中有一个简单route
的,如下所示:
Dash.PostsNewRoute = Em.Route.extend({
model: function() {
return this.store.createRecord('post');
},
actions: {
saveForm: function() {
this.modelFor('postsNew').save();
}
}
});
这是我为测试saveForm
并确保它被调用而编写的测试:
...
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();
});
});
但是,我收到此实现的错误:
Attempted to wrap undefined property actions.saveForm as function
如果我更改路线并且像这样进行测试,它将起作用:
// 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();
}
}
});
新测试:
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();
});
这样测试就会通过。可以actions.saveForm
直接测试吗?sinon 的限制是否阻止我访问 actions.saveForm?