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

时间:2013-06-03 05:43:06

标签: javascript jquery unit-testing bdd jasmine

我正在使用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();
    });
});

提前致谢。

1 个答案:

答案 0 :(得分:5)

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

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();
        });
    });
});
相关问题