gpt4 book ai didi

javascript - 在 Jasmine + Backbone 的 WebStorm 中运行 jsTestDriver

转载 作者:行者123 更新时间:2023-11-30 06:34:55 25 4
gpt4 key购买 nike

我正在用 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)

最佳答案

我认为最好使用 has模型对象的方法来检查属性,而不是检查 attributes 属性:

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

通过这种方式,您可以向模型添加一些隐式逻辑来覆盖 has 方法。此外,您没有指定如何扩展模型来创建 Cars 类。您是否指定了默认值?

关于javascript - 在 Jasmine + Backbone 的 WebStorm 中运行 jsTestDriver,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15184409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com