我有一个 BackboneView,我在创建时传递了一个集合
var my_collection = new Collection();
new MyView({temp: template, collection: my_collection });
在视图内部,在构造函数中的集合上调用 fetch() 方法,如下所示
export class MyView extends Backbone.View {
constructor(options){
this.collection = options.collection;
super();
this.collection.fetch().done(function(resp){
that.render();
})
}
render(){
}
}
我有(过去时)一系列测试,像这样测试视图的 UI/模板
describe('testing my view', function() {
it('should have one main child - div 6 ', function() {
expect(myview.el.children.length).to.equal(1);
});
});
但是,由于我现在添加了将集合传递给视图的代码,并在构造函数中调用了集合的 fetch 方法,所以我的所有测试都失败了,因为每次运行测试时视图都会尝试调用 fetch 方法。即使我传递了存根集合,我也必须设置 aurl
并且视图将尝试获取存根集合,从而导致 404
var gs = class StubCollection extends Backbone.Collection{
constructor(options){
this.url = '/blah';
}
}
const drv = new MyView({temp: template, collection: new gs()});
我需要在视图中的集合上调用 fetch(即我不能不使用该代码)。在这种情况下如何继续测试视图?