4

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

提前致谢 。

4

1 回答 1

5

我通常倾向于做的是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();
        });
    });
});
于 2013-06-03T06:07:28.730 回答