【问题标题】:Test for click event using Jasmine test suite使用 Jasmine 测试套件测试点击事件
【发布时间】:2013-05-29 05:49:20
【问题描述】:

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

这就是我所做的

 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();
    });
});

提前致谢。

【问题讨论】:

  • 我不认为..这需要 jquery 标记....现在删除它...
  • 我的意思是说这个问题被标记为 jquery ......我认为你的问题与它无关......所以我删除了 jquery 标签......
  • @bipen 保留 jquery 标记我认为没有害处。它可能有助于 OP 快速获得答案。
  • 我插入标签只是因为,我认为任何人都可以告诉我在 jquery 中没有任何按钮的情况下触发点击事件的方式,而无需关心 jasmine 和其他标签

标签: javascript jquery unit-testing bdd jasmine


【解决方案1】:

我通常倾向于使用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();
        });
    });
});

【讨论】:

    猜你喜欢
    • 2015-07-06
    • 2016-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-08
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    相关资源
    最近更新 更多