Sto provando a fare clic su un pulsante con il comando backbone.js, jasmine.js e sinon.js. Ma il seguente test fallisce. Sto usando una spia per controllare se viene chiamato o meno. Puoi aiutarmi per favore con questo?backbone.js click spia evento non viene richiamato utilizzando jasmine.js e sinon.js
Grazie.
Nuova operazione Template
<script id='new_task_template' type='text/template'>
<input type='text' id='new_task_name' name='new_task_name'></input>
<button type='button' id='add_new_task' name='add_new_task'>Add Task</button>
</script>
NewTaskView
T.views.NewTaskView = Backbone.View.extend({
tagName: 'section',
id: 'new_task_section',
template : _.template ($("#new_task_template").html()),
initialize: function(){
_.bindAll(this, 'render', 'addTask');
},
events:{
"click #add_new_task" : "addTask"
},
render: function(){
$(this.el).html(this.template());
return this;
},
addTask: function(event){
console.log("addTask");
}
});
Jasmine Test Case
describe("NewTaskView", function(){
beforeEach(function(){
this.view = new T.views.NewTaskView();
this.view.render();
});
it("should #add_new_task is clicked, it should trigger the addTask method", function(){
var clickSpy = sinon.spy(this.view, 'addTask');
$("#add_new_task").click();
expect(clickSpy).toHaveBeenCalled();
});
});
Jasmine uscita
NewTaskView
runEvents
runshould #add_new_task is clicked, it should trigger the addTask method
Expected Function to have been called.
Dove nel DOM fa la vista stessa render? Il modello è disponibile anche all'interno delle specifiche? Probabilmente il pulsante # add_new_task non esiste nel DOM del runner di specifiche di Jasmine e quindi '$ (" # add_new_task "). Click();' non ha alcun effetto. Se sei sicuro che la vista sia resa con il modello corretto, puoi usare l'elemento di NewTaskView come contesto per la funzione jquery: '$ ('# add_new_task', this.view.el) .click();'. –
Penso che questa domanda abbia già una risposta qui: http://stackoverflow.com/questions/8441612/why-is-this-sinon-spy-not-being-called-when-i-run-this-test/9012788# 9012788 – vadimich