茉莉花间谍没有被召唤 [英] Jasmine spies not being called

查看:119
本文介绍了茉莉花间谍没有被召唤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用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:


  1. 我是否以错误的方式测试此类事物?

  2. 为什么间谍没有注册该方法被调用的事实?


推荐答案

我刚刚验证了评论中概述的解决方案。你的描述应该是:

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 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆