对于我在 jasmine.js 版本 1.3.1 中的测试套件,夹具工作正常。 将 Jasmine.js 版本升级到 2.0.0 后,fixtures 不工作。
谁能解释一下,如何使我的代码适用于 jasmine.js 2.0.0 版中的固定装置?
我已经检查了这个 jasmine v2.0.0 发行说明,但没有与固定装置相关的内容:https://github.com/pivotal/jasmine/blob/v2.0.0/release_notes/20.md
最佳答案
下面的完整脚本对我来说工作正常,可以加载装置来测试您的 html 片段。
我已经引用了这个 url,这个 url 帮助我加载了固定装置:http://www.htmlgoodies.com/beyond/javascript/js-ref/testing-dom-events-using-jquery-and-jasmine-2.0.html
而且您还必须检查 jasmine.js v2.0.0 中的一些 Jasmine.js 框架语法
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Jasmine v2.0 DOM Tests Spec Runner</title>
<link rel="shortcut icon" type="image/png" href="images/jasmine_favicon.png">
<link rel="stylesheet" type="text/css" href="lib/jasmine-core/jasmine.css">
<script type="text/javascript" src="lib/jasmine-core/jasmine.js"></script>
<script type="text/javascript" src="lib/jasmine-core/jasmine-html.js"></script>
<script type="text/javascript" src="lib/jasmine-core/boot.js"></script>
<script type="text/javascript" src="lib/jasmine-core/jquery-1.7.2.js"></script>
<script type="text/javascript" src="lib/jasmine-core/jasmine-jquery.js"></script>
<script type="text/javascript">
var MSG = "Hello World!";
function hideMessage() {
$( "#pMsg" ).html("");
}
function showMessage() {
$( "#pMsg" ).html(MSG);
}
function setUpHTMLFixture() {
setFixtures('<form id="testForm" action="">'
+' <h1>Test Form</h1>'
+' <input type="text" id="txtMessage">'
+' <br>'
+' <button id="btnHideMessage" type="button" onclick="hideMessage()">Hide Message</button>'
+' <button id="btnShowMessage" type="button" onclick="showMessage()">Show Message</button>'
+' <br>'
+' <p id="pMsg"></p>'
+'</form>');
}
describe("DOM TESTS:***************", function() {
describe("Button Click Event Tests", function() {
var spyEvent;
beforeEach(function() {
setUpHTMLFixture();
});
it ("should invoke the btnShowMessage click event.", function() {
spyEvent = spyOnEvent('#btnShowMessage', 'click');
$('#btnShowMessage').trigger( "click" );
expect('click').toHaveBeenTriggeredOn('#btnShowMessage');
expect(spyEvent).toHaveBeenTriggered();
});
it ("should invoke the btnHideMessage click event.", function() {
spyEvent = spyOnEvent('#btnHideMessage', 'click');
$('#btnHideMessage').trigger( "click" );
expect('click').toHaveBeenTriggeredOn('#btnHideMessage');
expect(spyEvent).toHaveBeenTriggered();
});
});
describe("Show message tests", function() {
beforeEach(function() {
setUpHTMLFixture();
$('#txtMessage').val(MSG);
$('#btnShowMessage').trigger( "click" );
});
it ("should display the message when button is clicked.", function() {
expect($('#pMsg')).toHaveText($('#txtMessage').val());
});
});
describe("Hide message tests", function() {
beforeEach(function() {
setUpHTMLFixture();
$('#pMsg').text(MSG);
$('#btnHideMessage').trigger( "click" );
});
it ("should remove the message when button is clicked.", function() {
expect($('#pMsg')).toHaveText("");
});
});
});
</script>
</head>
<body>
</body>
</html>
关于javascript - 如何在 Jasmine.js 2.0.0 版本中使用 fixtures?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23680035/