Jquery.find()在主干视图中返回未定义
Im使用Jasmine.js在称为DialogView的主干视图中对函数进行单元测试。要在DialogView中测试的功能是:Jquery.find()在主干视图中返回未定义,jquery,backbone.js,jasmine,Jquery,Backbone.js,Jasmine,Im使用Jasmine.js在称为DialogView的主干视图中对函数进行单元测试。要在DialogView中测试的功能是: wireScroll: function() { var self = this; var wrapper = self.$el.find('.iscrollWrapper').get(0); ... }, 我想查一查,叫find it('jquery find called', functi
wireScroll: function() {
var self = this;
var wrapper = self.$el.find('.iscrollWrapper').get(0);
...
},
我想查一查,叫find
it('jquery find called', function () {
theView = new DialogView();
theView.render();
spyOn($.fn, 'find');
theView.wireScroll();
expect($.fn.find).toHaveBeenCalled();
});
然而,我发现了一个非常奇怪的问题:
Cannot read property 'get' of undefined
wireScroll: function() {
var self = this;
console.log('in wireScroll and self.$el[0].outerHTML is ');
console.log(self.$el[0].outerHTML);
console.log('in wireScroll, finding .iscrollWrapper');
console.log(self.$el.find('.iscrollWrapper'));
},
因此在视图中,.find失败,因此.get失败。所以我做了一些测试和日志:
it('jquery find called', function () {
theView = new DialogView();
theView.render();
console.log('in unit test and theView.$el[0].outerHTML is ');
console.log(theView.$el[0].outerHTML);
console.log('in unit test, finding .iscrollWrapper');
console.log(theView.$el.find('.iscrollWrapper'));
});
并且认为:
Cannot read property 'get' of undefined
wireScroll: function() {
var self = this;
console.log('in wireScroll and self.$el[0].outerHTML is ');
console.log(self.$el[0].outerHTML);
console.log('in wireScroll, finding .iscrollWrapper');
console.log(self.$el.find('.iscrollWrapper'));
},
可以预见,单元测试和视图中outerHTML的结果是相同的。两者都包含一个类为“iscrollWrapper”的div元素
在单元测试中,.find的日志结果为:
in unit test, finding .iscrollWrapper
[div.iscrollWrapper, prevObject: jQuery.fn.jQuery.init[1], context: undefined, selector: ".iscrollWrapper", constructor: function, init: function…]
然而,我认为:
in wireScroll, finding .iscrollWrapper
undefined
这是没有意义的,因为这两个的outerHTML日志都显示有一个带有iscrollWrapper类的div。你知道可能发生了什么吗?我在spyOn的结尾漏掉了andCallThrough。因此,它应该是:
spyOn($.fn, 'find').andCallThrough();
它正在工作