我有一个页面总结来自2个输入框的数字输入,并且我正在尝试对Jasmine进行的按钮单击事件进行监视的单元测试,但是Jasmine抛出了ReferenceError: spyOnEvent is not defined
。
我尝试加入jasmine-jquery
,但没有用。
这是页面:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Writing JavaScript test cases with Jasmine framework</title>
<meta charset="utf-8" />
<link href="Content/themes/base/jquery-ui.min.css" rel="stylesheet" />
<link href="Content/themes/base/base.css" rel="stylesheet" />
<script src="Scripts/jquery-3.4.1.min.js"></script>
<script src="Scripts/jquery-ui-1.12.1.min.js"></script>
<script src="test1.js"></script>
</head>
<body>
<input type="number" id="firstVal"> +
<input type="number" id="secondVal"> =
<input type="number" id="result"><br>
<input type="submit" id="submitRes">
</body>
</html>
这是正在使用和测试的JavaScript代码(test1.js
)
$(document).ready(function () {
$("#submitRes").click(sumUp);
});
function sumUp() {
$('#result').val(+$('#firstVal').val() + +$('#secondVal').val());
}
这是Jasmine SpecRunner:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Jasmine Spec Runner</title>
<link href="Content/jasmine/jasmine.css" rel="stylesheet" />
<script src="http://localhost:57596/Scripts/jquery-3.4.1.min.js"></script>
<script src="http://localhost:57596/Scripts/jquery-ui-1.12.1.min.js"></script>
<script src="Scripts/jasmine/jasmine.js"></script>
<script src="Scripts/jasmine/jasmine-html.js"></script>
<script src="Scripts/jasmine/jasmine-jquery.js"></script>
<script src="Scripts/jasmine/console.js"></script>
<script src="Scripts/jasmine/boot.js"></script>
<script src="Scripts/jquery/jquery-3.4.1.js"></script>
<script src="Scripts/indextests.js"></script>
<script src="http://localhost:57596/test1.js"></script>
</head>
<body>
</body>
</html>
这是测试JavaScript文件indextests.js
:
describe("Button Click Event tests: ", function () {
var spyEvent;
beforeEach(function () {
sumUp();
});
it("spy on button", function () {
spyEvent = spyOnEvent('#submitRes', 'click');
$('#submitRes').trigger('click');
expect('click').toHaveBeenTriggeredOn('#submitRes');
expect(spyEvent).toHaveBeenTriggered();
});
});