QUnit和Context

时间:2018-03-20 04:54:36

标签: javascript jquery qunit

尝试使用最初有效的QUnit上下文传递(http://api.qunitjs.com/QUnit/module),但似乎与子模块完全不一致。

在子模块之后运行下面的测试或模块似乎擦除了上下文。

QUnit.module('TEST #1', { 
  before: function() { 
    console.log('Test #1 - before: ', this); 
    this.abc = 123; 
  }
}, function() {
  // Run the top level test
  QUnit.test('Test A', function() {
    console.log("test A: ", this);
  });
    
  QUnit.test('Test B', function() {
    console.log("test B: ", this);
  });
    
  QUnit.test('Test C', function() {
    console.log("test C: ", this);
  });
});

QUnit.module('TEST #2', { 
  before: function() { 
    console.log('Test #2 - before: ', this); 
    this.abc = 123; 
  }
}, function() {
  // Defining a sub module
  QUnit.module('SUB-TEST', { 
    before: function() { 
      console.log('Test #2 - before (subtest): ', this);
      this.more = 456; 
    }
  }, function(t1) {
    // Run the second level test
    QUnit.test('SubTest', function() {
      console.log("subtest: ", this);
    });
  });
		
  // Run the top level test
  QUnit.test('Test', function() {
    // This is where 'this' is empty!!
    console.error("test (FAULTY HERE): ", this);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/qunit/2.4.1/qunit.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/qunit/2.4.1/qunit.min.css" rel="stylesheet"/>

我错过了什么,或者这是一个错误?

0 个答案:

没有答案