Test azioni a Ember.js con Sinon e Konacha
-
21-12-2019 - |
Domanda
Ho un semplice route
nella mia app come questo:
Dash.PostsNewRoute = Em.Route.extend({
model: function() {
return this.store.createRecord('post');
},
actions: {
saveForm: function() {
this.modelFor('postsNew').save();
}
}
});
.
Ecco il test che ho scritto per testare saveForm
e assicurati che sia stato chiamato:
...
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();
});
});
.
Tuttavia, ottengo un errore con questa implementazione:
Attempted to wrap undefined property actions.saveForm as function
Se cambio il percorso ed è il test in questo modo, funzionerà:
// 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();
}
}
});
.
Il nuovo 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();
});
.
In questo modo il test passerà.È possibile testare direttamente actions.saveForm
?La limitazione del sinone che mi impedisce di accedere alle azioni.Saveform?
Soluzione
Potresti prendere in giro le azioni hash e impostare l'aspettativa per questo tipo:
mock = sinon.mock(testHelper.lookup('controller', 'posts_new')._actions);
mock.expects('save').once();
this.submitButton.click();
mock.verify();
mock.restore();
. Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow