【问题标题】:beforeEach does not run in modular QUnit/Sinon tests with Require.js in CoffeeScriptbeforeEach 不能在 CoffeeScript 中使用 Require.js 的模块化 QUnit/Sinon 测试中运行
【发布时间】:2016-06-18 10:37:14
【问题描述】:

按照Nathan Davison here 的解释,尝试使用 require.js 构建 QUnit 0.9.0 测试,但另外使用 QUnit.module。 (coffeescript文件在打开测试页前转换成js文件)

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
    <link rel="stylesheet" href="css/qunit.css">
    <script data-main="unittestsmain" async src="js/require.js"></script>
</head>
<body>
    <div id="qunit"></div>
    <div id="qunit-fixture"></div>
</body>
</html>

unittestsmain.coffee(有 3 个测试模块,已删除以节省空间):

requirejs.config
  baseUrl: '.'
  paths:
    "sinon": 'js/sinon-1.17.3'
    "QUnit": 'js/qunit'
  shim:
    "QUnit":
      exports: 'QUnit',
      init: () ->
        QUnit.config.autoload = false
        QUnit.config.autostart = false

require ["QUnit", "sinon", "test_sum"],
  (QUnit, sinon, test_sum) ->
    test_sum.run()
    QUnit.load()
    QUnit.start()

test_sum.coffee(类似于QUnit documentation 中的示例之一,除了被包装到定义中):

define ["sum"], (sum) ->
  run: () ->
    module "Sum class",
      beforeEach: ->
        console.log "in beforeEach:", (p for p of @)
        @adder = sum.Sum()

    test "Sum: common cases", (assert) ->
      assert.equal sum.sum_of_entries([1, 2, 3]), 6
      assert.equal sum.sum_of_entries([]), 0
      return
    # ...
    test "Sum: adder class", (assert) ->
      console.log "in test:", (p for p of @)

      @adder.add(5)
      assert.equal @adder.result(), 0
    return

在添加 adder 类的测试之前我没有遇到任何问题,它在 Firefox 中抱怨:this.adder is undefined(并指向 test "Sum: adder class" ... 部分。)。在 Chromium 中:“TypeError: Cannot read property 'add' of undefined”,在同一个函数中。

浏览器中的测试页面看起来不错,除了上面提到的失败测试。

console.log 输出:in test: ["setup", "teardown", "beforeEach"](并且在日志中看不到“in beforeEach”。)

尝试了一些小的变化,例如 => 而不是 -> 、 .start 、 .load 顺序,但没有帮助。 define ["QUnit", "sum"], (QUnit, sum) -&gt; 然后在 test_sum 中使用 QUnit. 也无济于事。

我认为,最大的问题是为什么 beforeEach 不运行?我错过了什么吗?

【问题讨论】:

    标签: javascript coffeescript requirejs qunit sinon


    【解决方案1】:

    beforeEach 来自 QUnit 2.x,而在 QUnit 0.9 中它是 setup

    这些事情看完QUnit 2.x Upgrade Guide就清楚了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      • 2012-10-13
      • 2012-06-29
      • 2015-04-24
      相关资源
      最近更新 更多