gpt4 book ai didi

javascript - 监视 Jasmine-Jquery 函数调用

转载 作者:行者123 更新时间:2023-12-03 10:17:12 24 4
gpt4 key购买 nike

以下 jasmine-jquery 片段失败。我收到的错误是“预期 spy logIt 已被调用”。

var logIt = function () { 
console.log("logged");
};

$('#id1').on({
click: logIt
});

describe("Clicking id1", function() {
it("logs to the console.", function() {
spyOn(window, 'logIt');
$('#id1').click();
expect(window.logIt).toHaveBeenCalled();
});
});

//ERROR: "Expected spy logIt to have been called."

最佳答案

试试这个:

describe("Clicking id1", function() {
var logIt, $button;

beforeAll(function () {
logIt = jasmine.createSpy('logIt() spy');

$button = $('#id1');
$button.click(logIt);
$button.click();
});

it("logs to the console.", function () {
expect(logIt).toHaveBeenCalled();
});
});

我在我的项目中运行了以下测试并且它有效:

describe("Clicking id1", function() {
var $button, specHelper, logIt;

beforeAll(function () {
logIt = jasmine.createSpy('logIt() spy');
$button = $('<button>').attr("id", "id1");
specHelper = new SpecHelper();
specHelper.publish($button);

$button.click(logIt);
$button.click();
});

afterAll(function () {
specHelper.conceal($button);
});

it("logs to the console.", function () {
console.dir(logIt);
expect(logIt).toHaveBeenCalled();
});
});

specHelper.publish 将 jQuery 节点附加到 DOM,specHelper.conceal 将其删除。

关于javascript - 监视 Jasmine-Jquery 函数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29809837/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com