testFunc-6ren">
gpt4 book ai didi

javascript - 使用 Jasmine 测试 Javascript 函数

转载 作者:行者123 更新时间:2023-11-29 20:54:10 25 4
gpt4 key购买 nike

当我尝试测试一些 javascript 时遇到问题,我不确定代码是否真的可测试,但我很高兴知道如何使其可测试。

$("#Button").on('touchstart', () => testFunc());

function testFunc() {
button(true, userInfo, gameCode);
}

我想测试当触摸按钮时 testFunc 被调用。我使用 Jasmine 作为我的测试框架。

但是无论出于何种原因,当我尝试从 Jasmine 调用 testFunc 来测试是否正常工作时,我得到了错误

"ReferenceError: Can't find variable: testClass in file:///E:/Dokumenter/SemesterProjekt%20fun%20stuff/PRJ4Web/TankWebApplication/TankWebApplication/Test/Test.js (line 8)s"

我已经引用了该文件。所以我不确定发生了什么。

给我错误的测试代码

describe("Joystick test for functionallity",
function() {
beforeEach(function() {

});


it("All default values",
function() {
testFunc();
});
});

如何测试这段代码?可以吗?

最佳答案

您的目标是查看函数是否会在单击按钮时被调用,因此,您必须检查实际的函数调用toHaveBeenCalled Jasmine 方法。

单独调用函数没有任何意义(就像在您的示例中一样)

你应该这样做:

  it('All default values', => {

spyOn(class.testFunc);
document.getElementById('Button').click();

expect(class.testFunc).toHaveBeenCalled();
}));
})

关于javascript - 使用 Jasmine 测试 Javascript 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50191362/

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