我想使用JsTestDriver驾驶我的QUnit测试,但我对如何使用 #qunit-fixture
标记感到困惑。
标准 QUnit 测试运行程序是一个 HTML 页面。
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<link rel="stylesheet" href="/qunit/qunit.css" type="text/css" />
<script type="text/javascript" src="/qunit/qunit.js"></script>
<script type="text/javascript" src="test-script.js"></script>
</head>
<body>
...
<div id="qunit-fixture">
*** tests depend on markup that goes here ***
</div>
</body>
</html>
JsTestDriver 有一个 QUnitAdapter可用于加载和运行 test-script.js,但我没有看到任何在 #qunit-fixture
中获取标记的方法。
我错过了什么吗?适配器是否应该能够运行现有的 QUnit 测试?或者它只是将 QUnit 的断言框架与 JsTestDriver 结合使用的一种方法?
最佳答案
JsTestDriver 与其他一些测试框架的不同之处在于,它不使用外部 HTML 页面作为输入来提供 HTML 固定装置。
它提供了使用其 HtmlDoc 功能创建(或附加)HTML 片段作为测试用例定义的一部分的能力。
http://code.google.com/p/js-test-driver/wiki/HtmlDoc
这使得可以通过测试存储 HTML,例如:
TestCase("Test HtmlDoc Features", {
"test creating an isolated dom fragment": function() {
/*:DOC foo = <div id="demo"><p>foo</p></div>*/
assertEquals("Foo is a dom fragment with id 'demo'", this.foo.id, "demo");
},
"test appending HTML to the body": function() {
/*:DOC += <div id="demo"><p>foo</p></div>*/
assertEquals("HTML is appended to the body", document.getElementById("demo").innerHTML, "<p>foo</p>");
}
});
不幸的是,他们的方法存在一些局限性。例如,虽然内联样式可以直接应用于 HTML 标签,但将样式 block 或链接标签插入到外部 CSS 文件不会将 CSS 样式应用于生成的标记。这使得在您的测试用例中包含 CSS 变得很困难,对于您想要测试在 DOM 元素上获取或设置样式属性的测试。
关于javascript - 将 QUnit 与 JSTestDriver 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5105972/