茉莉花间谍不被称为 [英] 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 固定文件的一部分.
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>
滑块:
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:
- 我是否以错误的方式测试这种类型的东西?
- 为什么间谍没有记录该方法已被调用的事实?
推荐答案
我刚刚验证了评论中概述的解决方案.你的 describe
应该是:
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
函数和 before Slider
创建.
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');
直接在 slider
实例上创建滑块属性 handleClick
(包含 spy 对象).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
原型属性,您的点击事件绑定到该属性.这意味着刚刚触发的点击事件由原型 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屋!