我在Jasmine中为Backbone应用程序编写测试,我想知道我的测试涵盖的代码比例。为了这个目标,我想使用jsTestDriver。但我有一个问题:我创建了一个配置文件并在那里添加了所有资源,但是当我开始测试时,Backbone方法没有定义。这是我的配置文件:
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();
})
});
Cars是Backbone模型,此模型具有默认属性StartSate。在我的测试中,我想检查这个属性是否已定义。而且当然是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 :(得分:0)
我认为最好使用模型对象的has
方法来检查属性,而不是检查attributes
属性:
describe("Attributes", function(){
it("Test", function() {
c = new Cars;
expect(c.has("StartDate")).toBe(true);
})
});
这样,您可以向模型添加一些可以覆盖has
方法的隐式逻辑。此外,您没有指定如何扩展模型以创建Cars
类。您是否指定了默认值?