javascript 使用 Jasmine 测试套件测试点击事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16890618/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:23:15  来源:igfitidea点击:

Test for click event using Jasmine test suite

javascriptjqueryunit-testingbddjasmine

提问by Ancient

I am using jasmineto test my application and right now no button exists in my code
but i want to write a test in which i can check that whether a click event is fired or not.
You can simply think that i want to fire click event without button .

我正在jasmine用来测试我的应用程序,现在我的代码中不存在按钮,
但我想编写一个测试,在其中我可以检查是否触发了单击事件。
您可以简单地认为我想在没有按钮的情况下触发点击事件。

Here is what i did

这是我所做的

 scenario('checking that click event is triggered or not', function () {

    given('Sigin form is filled', function () {

    });
    when('signin button is clicked ', function () {
        spyOn($, "click");
        $.click();

    });
    then('Should click event is fired or not" ', function () {
        expect($.click).toHaveBeenCalled();
    });
});

Thanks in advance .

提前致谢 。

回答by Sushanth --

What I generally tend to do is create a stuband assign the event to the stub. Then trigger the click event and check if it was called

我通常倾向于做的是create a stub将事件分配给存根。然后触发点击事件并检查它是否被调用

describe('view interactions', function () {
    beforeEach(function () {
        this.clickEventStub = sinon.stub(this, 'clickEvent');
    });

    afterEach(function () {
        this.clickEvent.restore();
    });

    describe('when item is clicked', function () {
        it('event is fired', function () {
            this.elem.trigger('click');
            expect(this.clickEventStub).toHaveBeenCalled();
        });
    });
});