【问题标题】:Coffeescript unit test failing with Mocha, Should, Node.jsCoffeescript 单元测试因 Mocha、Should、Node.js 失败
【发布时间】:2014-12-13 16:11:33
【问题描述】:

我正在尝试使用 Mocha 为 CoffeeScript 示例类层次结构运行示例 CoffeeScript 单元测试。但我不断收到错误,似乎没有一些帮助我无法修复它们。这是我的示例 CoffeeScript 文件,位于 /src 文件夹中:

 #Animal.coffee
  class Animal
    constructor: (@name) ->

    move: (meters) ->
      console.log @name + " moved #{meters}m."

  class Snake extends Animal
    move: ->
      console.log "Slithering..."
      super 5

  class Horse extends Animal
    move: ->
      console.log "Galloping..."
      super 45

  #module.exports = new Snake()
  module.exports.Snake = Snake

这是 /Tests 文件夹中的 CoffeeScript 单元测试:

  #AnimalTest.coffee
  should  = require 'should'
  { Snake } = require "../src/Animal"

  describe 'sample', ->
     it 'should pass', ->
         snake = new Snake "Venomous python"
         snake.should.be.an.instanceOf(Snake)

我已经在全球范围内安装了所有这些库。所以当我通过命令行(Windows 7)执行这个命令时:

   Desktop>mocha --compilerscoffee:coffee-script/register Tests

它抛出了这个错误:

    desktop\Tests\sampleTest.coffee:15
    snake.should.be.an.instanceOf(Snake);
    ^
    ReferenceError: snake is not defined
    at Object.<anonymous> (c:\users\ap\desktop\Tests\AnimalTest.coffee:8:3)
    at Object.<anonymous> (c:\users\ap\desktop\Tests\AnimalTest.coffee:2:1)
    at Module._compile (module.js:456:26)
    at Object.loadFile (C:\Users\ap\AppData\Roaming\npm\node_modules\coffee-sc
    ript\lib\coffee-script\register.js:16:19)
    at Module.load (C:\Users\ap\AppData\Roaming\npm\node_modules\coffee-script
    \lib\coffee-script\register.js:45:36)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at C:\Users\ap\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:172:27
    at Array.forEach (native)
    at Mocha.loadFiles (C:\Users\ap\AppData\Roaming\npm\node_modules\mocha\lib
    \mocha.js:169:14)
    at Mocha.run (C:\Users\ap\AppData\Roaming\npm\node_modules\mocha\lib\mocha
    .js:356:31)
    at Object.<anonymous> (C:\Users\ap\AppData\Roaming\npm\node_modules\mocha\
    bin\_mocha:359:16)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

我使用的是 CoffeeScript 1.7.1 版、Mocha 1.18.2 版和 Node 0.10.26 版。我已经尝试到处寻找并尝试了测试之间的所有组合,但我无法弄清楚如何让它成功创建对象并运行测试。

作为参考,我尝试了以下所有其他组合: Requiring CoffeeScript file from within another CoffeeScript file with nodejs

How to check for class inheritance in Coffeescript Mocha Test?

有人能指出什么可能是错的吗?

更新 1: 修改了 dule 指出的所有内容,但现在我收到此错误:

module.js:340
    throw err;
      ^
    Error: Cannot find module 'should'
     at Function.Module._resolveFilename (module.js:338:15)
     at Function.Module._load (module.js:280:25)
     at Module.require (module.js:364:17)
     at require (module.js:380:17)
     at Object.<anonymous> (C:\Users\ap\Desktop\Tests\AnimalTest.coffee:2:11)
     at Object.<anonymous> (C:\Users\ap\Desktop\Tests\AnimalTest.coffee:2:1)
     at Module._compile (module.js:456:26)
     at Object.loadFile (C:\Users\ap\AppData\Roaming\npm\node_modules\coffee-sc
     ript\lib\coffee-script\register.js:16:19)
     at Module.load (C:\Users\ap\AppData\Roaming\npm\node_modules\coffee-script
     \lib\coffee-script\register.js:45:36)
     at Function.Module._load (module.js:312:12)
     at Module.require (module.js:364:17)
     at require (module.js:380:17)
     at C:\Users\ap\AppData\Roaming\npm\node_modules\mocha\lib\mocha.js:172:27
     at Array.forEach (native)
     at Mocha.loadFiles (C:\Users\ap\AppData\Roaming\npm\node_modules\mocha\lib
     \mocha.js:169:14)
     at Mocha.run (C:\Users\ap\AppData\Roaming\npm\node_modules\mocha\lib\mocha
     .js:356:31)
     at Object.<anonymous> (C:\Users\ap\AppData\Roaming\npm\node_modules\mocha\
     bin\_mocha:359:16)
     at Module._compile (module.js:456:26)
     at Object.Module._extensions..js (module.js:474:10)
     at Module.load (module.js:356:32)
     at Function.Module._load (module.js:312:12)
     at Function.Module.runMain (module.js:497:10)
     at startup (node.js:119:16)
     at node.js:902:3

更新 2:我对 AnimalTest 脚本进行了以下更改,测试通过了。

  should  = require ("../npm/node_modules/should/should")
  { Snake } = require ("../src/Animal.coffee")

  describe 'sample', ->
     it 'should pass', ->
    console.log(new Snake "Venomous python")

但是,由于某种原因,创建对象并将其保存以供使用,仍然失败。我得到同样的snake not defined 错误。

【问题讨论】:

  • snake 未定义,因为您不需要正确的文件,requires 是相对于当前文件,而不是相对于项目根目录(请参阅下面的答案),但那是这不是你唯一的问题......

标签: unit-testing coffeescript mocha.js should.js


【解决方案1】:

看起来你有很多问题。请尝试以下操作:

src/animal.coffee:

class Animal
    constructor: (@name) ->

    move: (meters) ->
        console.log @name + " moved #{meters}m."

class Snake extends Animal
    move: ->
        console.log "Slithering..."
        super 5

module.exports.Snake = Snake

测试/animalTest.coffee

should  = require "should"
{ Snake } = require "../src/animal"

describe 'sample', ->
    it 'should pass', ->
        snake = new Snake "Venomous python"
        snake.should.be.an.instanceOf(Snake)

然后运行(从项目根目录):

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

【讨论】:

  • 只是列举一些你原来的问题:(1)你对Snake进行instanceof检查但导入SnakeObj,(2)你不需要在导入节点模块时执行完整的相对路径比如should,(3)不知道你为什么要使用new Snake()导出一个蛇对象,(4)coffee-script的编译器应该是coffee-script而不是coffeescript
  • 1) 我尝试按原样导入 Snake,但没有成功。所以接下来我尝试导入 SnakeObj,如链接的堆栈溢出问题之一所示。 2)我尝试按原样导入节点模块,但找不到应该。我已经在全球范围内安装了所有库,所以我认为这可能是它无法找到它们的原因。 3)这与链接问题中提到的相同。 4)这是一个复制粘贴,但不知道连字符是如何丢失的。我重新检查了我的命令行上有连字符。
  • (1) 应该没问题,只要你在文件中保持一致,(2) 应该安装在项目本地 (npm install --save should),(3)链接的问题由提出问题的同一个人回答,看起来就像他忘记在答案中删除的一行
  • 我进行了所有更改并得到了上面更新的错误。也做了npm install --save should 然后运行命令。
【解决方案2】:

所以,这很奇怪。一个解释是非常受欢迎的!这就是我所做的并且测试通过了:

 should  = require ("../npm/node_modules/should/should")
 { Snake } = require ("../src/Animal.coffee")

 describe 'sample', ->
    it 'should pass', ->
      (new Snake "Venomous python").should.be.an.instanceOf(Snake)

【讨论】:

  • 这很愚蠢,但让我解释一下。我使用 Sublime Text 2 作为我的编辑器。出于某种原因,它无法理解我使用蛇对象的行上的缩进,尽管对我来说它看起来与创建它的位置正确对齐。因此,下一个对象超出了范围。一点点擦除和重写就可以了。它现在有效!哎呀!基于缩进的作用域!
  • 会不会是你有制表符而不是空格(反之亦然)?
  • 可能是,我不确定。我是 coffeescript 和整个 Node 环境的新手。所以,我认为这个问题是我不知道的,帮助我的人一直告诉我要求有问题。我想,否则,这个问题甚至不会落在这里,我最终会想出来的。
【解决方案3】:

我在使用 CoffeeScript 1.8.0、mocha 1.21.4 时随机损坏了测试用例。

测试用例可以手动批准,但在 mocha 中失败,出现“未定义函数...”等奇怪异常。复制long long 失败、异常、堆栈跟踪太琐碎...

解决它:

  • 享受麻烦
  • 注释掉所有失败的测试用例
  • 尝试更多直接使用javascript
  • 尝试使用其他单元测试工具,如nodeunit
  • 这听起来很愚蠢,但是当调试变得糟糕时,一切都会发生,至少对我来说是这样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-03-23
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 2013-07-06
    • 2018-11-05
    • 1970-01-01
    • 2022-11-03
    相关资源
    最近更新 更多