茉莉花间谍没有被召唤 [英] Jasmine spies not being called
问题描述
我在使用Jasmine的间谍方面遇到了一些麻烦
I am having some trouble implimenting spying in Jasmine
我想检查是否使用jasmine spy和jasmine jquery在滑块上点击了一个链接。
I want to check if a link has been clicked on a slider using a jasmine spy and jasmine jquery.
这是一个简化版本:
我有一些链接作为html fixture文件的一部分。
I have some links as part of an html fixture file.
<a href="#" class="someLink">Link 1</a>
<a href="#" class="someLink">Link 2</a>
slider:
var Slider = function(links){
this.sliderLinks = $(links);
this.bindEvents();
}
Slider.prototype.bindEvents = function(){
this.sliderLinks.on('click', this.handleClick);
}
Slider.prototype.handleClick = function(e){
console.log('i have been clicked')
}
规格文件:
describe('Slider', function(){
var slider;
beforeEach(function(){
loadFixtures('slider.html');
slider = new Slider('.someLink');
});
it('should handle link click', function(){
spyOn(slider, 'handleClick');
$(slider.sliderLinks[0]).trigger('click');
expect(slider.handleClick).toHaveBeenCalled();
});
});
测试失败。但是我已被点击已被记录到控制台,因此正在调用该方法。
The test is failing. But the 'i have been clicked' has been logged to the console so the method is being called.
如果我这样做,测试通过:
If I do this the test passes though:
it('should handle link click', function(){
spyon(slider, 'handleClick');
slider.handleClick();
expect(slider.handleClick).toHaveBeenCalled();
});
所以我的问题基本上是:
So my question essentially is:
- 我是否以错误的方式测试此类事物?
- 为什么间谍没有注册该方法被调用的事实?
推荐答案
我刚刚验证了评论中概述的解决方案。你的描述
应该是:
I've just verified the solution outlined in the comment. Your describe
should be:
describe('Slider', function () {
var slider;
beforeEach(function () {
loadFixtures('slider.html');
spyOn(Slider.prototype, 'handleClick');
slider = new Slider('.someLink');
});
it('should handle link click', function (){
$(slider.sliderLinks[0]).trigger('click');
expect(slider.handleClick).toHaveBeenCalled();
});
});
重点是你必须监视原型 handleClick $ c在
Slider
创建之前$ c> function和。
The point is that you have to spy on prototype handleClick
function and before the Slider
creation.
原因是Jasmine spyOn
在您提供的代码中确实如此:
The reason is what Jasmine spyOn
really does in the code you provided:
spyOn(slider, 'handleClick');
创建滑块属性 handleClick
(包含间谍)对象)直接在滑块
实例上。 slider.hasOwnProperty('handleClick')
在这种情况下返回 true
,你知道......
creates slider property handleClick
(containing the spy object) directly on the slider
instance. slider.hasOwnProperty('handleClick')
in this case returns true
, you know...
但是,还有 handleClick
您的点击事件绑定到的prototype属性。这意味着刚触发的click事件由原型 handleClick
函数处理,而滑块对象拥有属性 handleClick
(你的间谍)保持不变。
But still, there is handleClick
prototype property to which your click event is bound. That means just triggered click event is handled by the prototype handleClick
function while the slider object own property handleClick
(your spy) stays untouched.
所以答案是间谍没有注册这个方法已被调用的事实,因为它从未被调用过: - )
So the answer is that the spy is not registering the fact that the method has been called because it has never been called :-)
这篇关于茉莉花间谍没有被召唤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!