【问题标题】:Writing Mocha tests with IcedCoffeeScript?使用 IcedCoffeeScript 编写 Mocha 测试?
【发布时间】:2013-06-25 02:18:02
【问题描述】:

我正在尝试在 Mocha 测试中运行一些数据库查询,但遇到了一些问题。

这是测试(使用 Mongoose):

it.only "should create some objects", (done) ->
  await models.MyModel1.count defer(err, oldModel1Count)
  await models.MyModel2.count defer(err, oldModel2Count)

  # ... do some stuff

  await models.MyModel1.count defer(err, newModel1Count)
  await models.MyModel2.count defer(err, newModel2Count)

  assert.equal oldModel1Count + 1, newModel1Count
  assert.equal oldModel2Count + 1, newModel2Count

运行测试的命令:

mocha --compilers coffee:iced-coffee-script --require iced-coffee-script --require mocha --colors --recursive test"

错误发生在第一行:

ReferenceError: err is not defined

我只能假设它正在尝试使用普通的 CoffeeScript 来执行此代码,因此它认为 defer 是一个函数并尝试评估 err

是否可以在 IcedCoffeeScript 中编写 Mocha 测试?

【问题讨论】:

    标签: coffeescript mocha.js iced-coffeescript


    【解决方案1】:

    这对我有用

    mocha --require ./fix_my_iced_tests.js --compilers coffee:coffee-script
    

    创建 fix_my_iced_tests.js

    require('iced-coffee-script').register()
    

    创建 test/some_test.coffee(确保修复确实有效)

    assert = require 'assert'
    
    describe 'test section', ()->
      it 'is ok', (done)->
        await setTimeout (defer next), 100
        assert.strictEqual(1, 1)
        done()
        return
      return
    

    你应该收到这样的东西

      Type subdsl
        √ is ok (102ms)
    
    
      1 passing (109ms)
    

    如果没有修复,你应该会收到这样的东西

    ReferenceError: next is not defined
    

    ---已编辑---

    找到了更好的选择here

    mocha --compilers coffee:iced-coffee-script/register
    

    【讨论】:

      【解决方案2】:

      不确定这是否仍然相关,但现在是 2015 年,Node.js 对 Promises 和 Generators 提供一流的支持,这使您能够将代码exactly as concise and elegant 编写为 IcedCoffeeScript,但皱纹少了很多。

      【讨论】:

        猜你喜欢
        • 2015-03-07
        • 1970-01-01
        • 1970-01-01
        • 2015-08-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-24
        • 2023-03-31
        相关资源
        最近更新 更多