单击事件调用的Jasmine-Jquery Testing函数

时间:2018-08-22 21:38:43

标签: jquery jasmine karma-jasmine jasmine-jquery

我有一个测试规范,我试图在该规范上测试调用按钮时是否调用了函数。

这是正在测试的javascript:

var launchTest = function () {
    window.open("test.asp", "test", "resizable=1,scrollbars=yes,status=no,toolbar=no,location=no,menu=no");
};

//launch test window when btn is clicked
$('#testBtn').click(function () {
    launchTest ();
});

这是茉莉花:

describe('launchTest', function () {
    it("opens a window", function () {
        var windowSpy = spyOn(window, "open");

        launchTest();

        expect(windowSpy).toHaveBeenCalledWith("test.asp", "test", "resizable=1,scrollbars=yes,status=no,toolbar=no,location=no,menu=no");
    });
});

describe('testBtn click event', function () {
    it('should call launchTest.', function () {
        $('body').append('<input type="button" id="testBtn" />');

        var launchSpy = spyOn(window, 'launchTest');

        $('#testBtn').trigger('click');

        expect(launchSpy).toHaveBeenCalled();
    });
});

此操作始终失败,提示“已调用预期的间谍launchTest”。

正如标题中所述,我正在使用jasmine-jquery。我知道如何测试点击事件是否已触发,但不测试定义的点击事件中调用的函数。

0 个答案:

没有答案
相关问题