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

Anc*_*ent 4 javascript bdd jquery unit-testing 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();
    });
});
Run Code Online (Sandbox Code Playgroud)

提前致谢 .

Sus*_* -- 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();
        });
    });
});
Run Code Online (Sandbox Code Playgroud)