2012-04-13 50 views

回答

3

如果您的視圖本身生成el,則可以通過視圖的$el輕鬆觸發事件。


describe("my test", function(){ 

    var MyModel = Backbone.Model.extend({}); 

    var MyView = Backbone.View.extend({ 
    events: { 
     "click a": "aClicked" 
    }, 

    aClicked: function(e){ 
     e.preventDefault(); 
     this.model.set({foo: "bar"}); 
    }, 

    render: function(){ 
     this.$el.html("<a href='#foo' id='fooLink'>foo</a>"); 
    } 
    }); 

    var myModel; 

    beforeEach(function(){ 
    myModel = new MyModel(); 
    var myView = new MyView({ 
     model: myModel 
    }); 

    myView.render(); 

    // this will "click" the link 
    myView.$("#fooLink").trigger("click"); 
    }) 

    it("should do that stuff", function(){ 
    expect(myModel.get("foo")).toBe("bar"); 
    }); 

}); 
+0

太棒了!但是我怎麼能夠窺探模型已經設置? – chchrist 2012-04-13 13:01:10

+0

我寫的測試顯示...或者我不明白問題 – 2012-04-13 13:03:39

+0

它觸發事件,但它不檢查模型是否已被調用 – chchrist 2012-04-13 13:37:14

相關問題