首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >访问Qunit模块设置变量

访问Qunit模块设置变量
EN

Stack Overflow用户
提问于 2017-07-25 18:31:27
回答 2查看 448关注 0票数 1

嗨,伙计们,目前我正在用Qunit测试框架测试我的javascript代码。我无法访问QUnit.module函数中的QUnit.test设置变量。

代码语言:javascript
复制
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未定义

EN

回答 2

Stack Overflow用户

发布于 2017-07-25 18:50:12

根据注释,如果您只想保留HTML元素,您可以完全在模块之外创建一个变量。使用this并不真正有效(据我所知):

代码语言:javascript
复制
(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 !" );
  });

})();
票数 0
EN

Stack Overflow用户

发布于 2017-07-25 20:22:25

代码语言:javascript
复制
(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 !" );
  });

})();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45311078

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档