Backbone.js 使用mocha进行主干收集初始化功能测试
如何为Backbone.js 使用mocha进行主干收集初始化功能测试,backbone.js,mocha.js,sinon,chai,Backbone.js,Mocha.js,Sinon,Chai,如何为主干.Collection的初始化函数的选项.url参数编写测试 我有以下代码,在编写测试时遇到了问题。我用的是Mocha、Chai和Sinonjs。当我在主干集合中注释掉this.url=options.url时,测试通过,但当我取消注释时,我得到以下断言错误: TypeError:“未定义”不是对象(正在评估“options.url”) 我的主干代码: Foo.Collection._entity = Backbone.Collection.extend({ url: '',
主干.Collection
的初始化
函数的选项.url
参数编写测试
我有以下代码,在编写测试时遇到了问题。我用的是Mocha
、Chai
和Sinonjs
。当我在主干集合中注释掉this.url=options.url时,测试通过,但当我取消注释时,我得到以下断言错误:
TypeError:“未定义”不是对象(正在评估“options.url”)
我的主干代码:
Foo.Collection._entity = Backbone.Collection.extend({
url: '', //API's endpoint for this collection
fetched: false, //flag to avoid fetching twice if consecutive fetches occur
/**
* Makes sure the URL is set
* @param {Object[]} models Set of initial models
* @param {Object} options Regular initialization options object
*/
initialize: function (models, options) {
models = models || [];
this.url = options.url;
}
});
我对这个集合的测试
describe("Foo.Collection._entity", function () {
beforeEach(function () {
// Sinon fake server for the backend
this.server = sinon.fakeServer.create();
// Server automatically responds to XHR requests
this.server.autoRespond = true;
this.entity = new Foo.Collection._entity();
});
afterEach(function () {
// stop fake server
this.server.restore();
});
describe("Default values", function () {
// test default values
it("has default values", function () {
expect(this.entity).to.be.ok;
expect(this.entity).to.have.length(0);
});
it("default url to be ' '",function() {
var entity = this.entity;
expect(entity.url).to.be.equal("");
});
it("fetched flag to be set to false", function (){
var entity = this.entity;
expect(entity.fetched).to.be.equal(false);
});
});
});
如何编写一个测试来通过
this.url=options.url
代码段?事实证明,它非常简单,只需在每个函数之前使用中的正确参数实例化集合,然后测试url和集合的长度
this.entity = new Foo.Collection._entity({options:
{
url: "/Foo"
}
});
describe("Default values", function () {
// test default values
it("has default values", function () {
expect(this.entity).to.be.ok;
expect(this.entity).to.have.length(1);
expect(entity.url).to.be.equal("Foo");
});