Estoy tratando de probar un clic del botón usando backbone.js, jasmine.js y sinon.js. Pero el siguiente caso de prueba falla. Estoy usando un espía para rastrear si se llama o no. ¿Puedes ayudarme con esto?backbone.js haga clic en el evento espía no se llama usando jasmine.js y sinon.js
Gracias.
tarea nueva plantilla
<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 caso de prueba
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 salida
NewTaskView
runEvents
runshould #add_new_task is clicked, it should trigger the addTask method
Expected Function to have been called.
¿En qué el DOM en el render en sí? ¿La plantilla está disponible dentro de las especificaciones? Probablemente el botón # add_new_task no existe en el DOM del corredor de especificaciones Jasmine y, por lo tanto, '$ (" # add_new_task "). Click();' no tiene ningún efecto. Si está seguro de que la vista se representa con la plantilla correcta, podría usar el elemento NewTaskView como contexto para la función jquery: '$ ('# add_new_task', this.view.el) .click();'. –
Creo que esta pregunta ya está respondida aquí: http://stackoverflow.com/questions/8441612/why-is-this-sinon-spy-not-being-called-when-i-run-this-test/9012788# 9012788 – vadimich