我想借此@ Gepser的解決方案遠一點(但可以肯定的是解決方案的一部分)。如果您想通過名稱獲取表單,那麼您可能希望在每次測試之前使用QUnit的夾具來重置HTML。那麼你可能想要剔除alert
方法,以便在測試時不會得到一堆。
在QUnit HTML文件:
<body>
<div id="qunit"></div>
<div id="qunit-fixture">
<!-- Anything in here gets reset before each test -->
<form name="formSecond">
<input type="text" name="nameFull">
</form>
</div>
...
</body>
然後在你的QUnit測試(無論是在HTML文件中我們在自己的JS文件):
QUnit.begin(function() {
// mock out the alert method to test that it was called without actually getting an alert
window.alert = function() {
window.alert.called++;
};
window.alert.called = 0;
});
QUnit.testDone(function() {
// reset the alert called count after each test
window.alert.called = 0;
});
...
// From @Gepser's answer...
QUnit.test("CheckingName", function(assert) {
var value = false;
assert.equal(value, validNameCheck(), "We expect the return to be false");
// add an assertion to make sure alert was called
assert.equal(1, window.alert.called, "alert was called only once");
});
的參數'form'似乎是沒用過 – 2014-09-29 15:49:52