2013-03-03 63 views
2

我正在爲Jasmine爲Backbone應用程序編寫測試,我想知道我的測試覆蓋了多少代碼。爲了這個目標,我想使用jsTestDriver。但我有一個問題:我創建了一個配置文件並在那裏添加了所有資源,但是當我開始測試時,Backbone方法不定義。這是我的配置文件:在WebStorm中爲Jasmine + Backbone運行jsTestDriver

server: http://localhost:9876 

load: 
    - lib/jasmine-1.3.1/jasmine.js 
    - lib/jasmine-jquery.js 
    - lib/JasmineAdapter.js 
    - lib/sinon-1.5.2.js 
    - cordova-2.2.0.js 
    - libs/jquery-1.8.2.min.js 
    - libs/underscore-min.js 
    - libs/backbone-min.js 
    - libs/lazyload-min.js 
    - core/js/core.js 
    - index.js 

test: 
    - spec/test.js 

順序與SpecRunner文件上的順序相同。這是我的測試文件:

describe("Attributes", function(){ 
    it("Test", function() { 
     c = new Cars; 
     expect(c.attributes.StartDate).toBeDefined(); 
     expect(c.attributes.StartDate).toBeDefined(); 
    }) 
}); 

汽車是Backbone模型,此模型具有默認屬性StartSate。在我的測試中,我想檢查是否定義了這個屬性。和ofcourse在WebStorm錯誤:

TypeError: TypeError: Cannot read property 'attributes' of undefined 
TypeError: Cannot read property 'attributes' of undefined 
      at null.<anonymous> (spec/test.js:10:21) 
+0

什麼是'Model'?它不應該是'Backbone.Model'嗎? – neebz 2013-03-03 11:50:45

+0

是的,當然,我在關於我的測試的文本中添加了描述 – user1802967 2013-03-03 20:17:50

回答

0

我認爲這是更好地使用模型對象的has方法來檢查的屬性,而不是檢查attributes屬性:

describe("Attributes", function(){ 
    it("Test", function() { 
     c = new Cars; 
     expect(c.has("StartDate")).toBe(true); 
    }) 
}); 

這樣你可以向模型添加一些隱式邏輯,以覆蓋has方法。另外,您沒有指定如何擴展模型以創建類Cars。你有沒有指定默認值?

相關問題