Testing backbone.js application with jasmine - how to test model bindings on a view?

前端 未结 6 1591
-上瘾入骨i
-上瘾入骨i 2020-12-24 02:48

I had some interesting tribulations in trying to test whether views were correctly bound to events.  In backbone, we typically bind to events in the initialize method, using

6条回答
  •  [愿得一人]
    2020-12-24 03:22

    I solved this problem by spying on a function called by my render function. So in your example:

    myView = Backbone.View.extend({
      initialize: function(){
          _.bindAll(this, "render");
          something.bind("change", this.render);
      },
      someOtherFunction: function(){},  //this function only called from render
      render: function(){ this.someOtherFunction(); /* rest of render function */ }
    });
    

    test looks like:

    this.myView = new MyView();
    spyOn(this.myView, "someOtherFunction");
    this.myView.something.trigger("change");
    expect(this.myView.someOtherFunction).toHaveBeenCalled();  
    

    then I wrote a separate test for whatever someOtherFunction does.

提交回复
热议问题