我对Jasmine测试很陌生,我面临一个小问题。我有这样的功能:
“redirectPage.js”
function clickThis(){
document.getElementById("link").click();
}
如何测试我的测试中是否已调用/触发此功能?
这是我目前的测试:
describe('Test redirectPage', function() {
beforeEach(function() {
loadFixtures("redirectPageFixture.html");
});
it(" checks if the clickThis() method is triggered", function(){
var spyEvent = spyOnEvent("#link", "click");
("#link").trigger("click");
expect(spyEvent).toHaveBeenTriggered();
});
});
但是这样做,给了我这个错误:
TypeError: "#link".click is not a function
如何修复此测试? 非常感谢任何帮助!
更新:
这是我的装备:
<html>
<head>
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap-theme.min.css">
<script src="http://code.jquery.com/jquery.min.js" defer>
<link rel="stylesheet" href="css/navbar.css">
<link rel="stylesheet" href="css/searchControl.css">
<script type="text/javascript" src="src/main/resources/assets/js/redirectPage.js" defer>
<script src="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>
</head>
<body onload="setTimeout('clickThis();',3000);>
<p><a id=link href = "www.google.com">click here</a></p>
</body>
</html>
答案 0 :(得分:2)
我终于弄清楚我做错了什么。我在我的对象上创建了间谍,但实际上并没有调用我需要的功能。如果有人感兴趣,这是我现在的测试用例:
it(" checks if the clickThis() method is triggered", function(){
var spyEvent = spyOnEvent("#link", "click");
clickThis();
expect(spyEvent).toHaveBeenTriggered();
});
感谢大家的投入! :)