嗨,伙计们,目前我正在用Qunit测试框架测试我的javascript代码。我无法访问QUnit.module函数中的QUnit.test设置变量。
QUnit.module( "Module A:Build Notes",{
setup: function () {
this.inputsticky = $("input[name=stickyinput]");
}
});
QUnit.test("Test Case 1",function (assert) {
assert.expect(1);
orangeClick(); //changing color
assert.equal( this.inputsticky.css('background-color'),'rgb(255, 165, 0)', "orange Function passed !" );
});结果: this.inputsticky未定义
发布于 2017-07-25 18:50:12
根据注释,如果您只想保留HTML元素,您可以完全在模块之外创建一个变量。使用this并不真正有效(据我所知):
(function() {
// put things in an IIFE to prevent data leakage
let inputElement; // define the variable at a higher scope
QUnit.module( "Module A:Build Notes",{
setup: function () {
// now we can set the variable's value for use in all later tests
inputElement = $("input[name=stickyinput]");
}
});
QUnit.test("Test Case 1",function (assert) {
assert.expect(1);
orangeClick(); //changing color
assert.equal( inputElement.css('background-color'),'rgb(255, 165, 0)', "orange Function passed !" );
});
})();发布于 2017-07-25 20:22:25
(function() {
var inputElement; // define the variable at a higher scope
QUnit.module( "Module A:Build Notes",{
beforeEach: function () {
// now we can set the variable's value for use in all later tests
inputSticky = $("input[name=stickyinput]");
}
});
QUnit.test("Test Case 1",function (assert) {
assert.expect(1);
orangeClick(); //changing color
assert.equal( inputSticky.css('background-color'),'rgb(255, 165, 0)', "orange Function passed !" );
});
})();https://stackoverflow.com/questions/45311078
复制相似问题