dom测试
dom测试的配置,在搞这个时候,费了点劲,但是真正配置起来超简单。
require
由于用了jasmine
,所以就需要有与jasmine配套的,本想找个仅仅依赖jasmine
的库,但失败了,就配了个依赖jquery的。
jquery-1.11.0.js
jasmine-jquery.js
jasmine-ajax.js //ajax模拟
配置
Gruntfile.js
下
jasmine: {
defualt: {
src: 'pub/*.js',
options: {
vendor: [//在vendor里添加测试所依赖的库即可
"libs/jquery-1.11.0.js",
"libs/jasmine-jquery.js",
"libs/jasmine-ajax.js"
],
specs: 'test/*Spec.js'
}
}
}
代码样例
describe("bind", function() {
// jasmine.getFixtures().fixturesPath = "test/fixtures/";//设定存放测试html的文件夹
var fixture, bind;
var Bind = bindProvider();//自己封装的东西
beforeEach(function() {
fixture = setFixtures('<div id="bind">foo</div>');
bind = new Bind(fixture);
bind.bind("aa", "bbb");
});
it("should bind key/value to the dom", function() {
//var div = setFixtures('<div class="post">foo</div>');
// var bind = new Bind(fixture);
//bind.bind("aa", "bbb");
expect(bind.hasBind("aa")).toBe(true);
});
it("can get the binded thing", function() {
expect(bind.get("aa")).toEqual("bbb");
});
it("can unbind", function() {
bind.unbind("aa");
expect(bind.hasBind("aa")).toBe(false);
});
});
后记
具体测试api,可参见jasmine-jquery和jasmine-ajax,都蛮简单的,不做细说。