2014-06-10 27 views
0

我有一個Backbone應用程序,我正在研究所有功能都在App下的命名空間。我有一個叫App.LoginView的視圖,我想用Jasmine來測試它,但是我正在努力讓它工作。檢查使用Jasmine調用的骨幹視圖方法

這是測試代碼:

describe("Test the views", function() { 
    'use strict'; 

    beforeEach(function() { 
     // Create DOM element 
     $('body').append('<div class="content"></div>'); 
    }); 

    afterEach(function() { 
     $('div.content').remove(); 
    }); 

    // Test the LoginView object was created 
    it("tests that the LoginView object exists", function() { 
     // Check the view definition exists 
     expect(App.LoginView).toBeDefined(); 

     // Spy on the prototype 
     spyOn(App, 'LoginView').andCallThrough(); 

     // Create the view 
     this.LoginView = new App.LoginView(); 

     // Check the view exists 
     expect(this.LoginView).toBeDefined(); 
     expect(this.LoginView.initialize).toBeDefined(); 
     expect(this.LoginView.template).toBeDefined(); 
     expect(this.LoginView.tagName).toBeDefined(); 
     expect(this.LoginView.render).toBeDefined(); 

     // Remove it 
     this.LoginView.remove(); 
    }); 
}); 

它引發以下錯誤:

TypeError: 'undefined' is not a function (evaluating 'spyOn(App, 'LoginView').andCallThrough()') 

我使用grunt-contrib-jasmine運行測試和jasmine-jquery增加對jQuery的支持。過去我曾經使用過茉莉花,但是我正努力去看看我在這裏出了什麼問題。

回答

1

最終找到了解決方案。得到它的工作,我窺探原型的initialize方法,改變呼叫的語法通過,因爲這在茉莉花改變2.

spyOn(App.LoginView.prototype, 'initialize').and.callThrough(); 

我還動態插入使用jasmine-jquery另有一個基本的模板到DOM下劃線抱怨模板丟失:

$('body').append('<script type="text/template" id="login-template"><div class="content-padded"></div></script>');