我正在使用Jasmine-jQuery对我的Javascript进行单元测试,但是无法让这个测试工作。我需要确认,当用户滚动页面足够远时,其中一个面板变得粘稠。这种情况发生在Lozenges.makeFixed()
方法中,因此我需要确认在窗口scroll
上调用此方法。我的测试失败了,不确定我做错了什么。 src代码是:
mycompany.singleJob=(function($,undefined) {
// some code goes here Lozenges object etc.
function init() {
// more code here
$(window).on("scroll.singleJob",function(e)
{
// at this point if its mobile device simply exit
if(mycompany.framework.isMobile()) return;
// we also need to make first panel sticky
// check if nav is getting out of visible area. it means it should be sticky
if(!LozengesPanel.fixed && $(window).scrollTop()>104)
{
LozengesPanel.makeFixed();
}
else if(LozengesPanel.fixed && $(window).scrollTop()<104)
{
LozengesPanel.makeStatic();
}
});
// more code here
}
// more code here
return {
init:init
};
}(jQuery));
现在,我想测试当用户滚动窗口时,将调用某个点LozengesPanel.makeFixed();
。我有这个测试套件,但它一直都失败了。
describe("SingleJob page", function() {
beforeEach(function() {
mycompany.singleJob.init();
});
it("panel sticks to top when page scrolled down", function() {
spyOn(mycompany.singleJob.LozengesPanel, "makeFixed");
$(window).scrollTop(500);
expect(mycompany.singleJob.LozengesPanel.makeFixed).toHaveBeenCalled();
});
});
我认为$(window).scrollTop(500)
应该触发滚动,为什么expect
行会失败?