Coffeescript单元测试失败了Mocha,应该,Node.js

时间:2014-04-14 19:39:50

标签: unit-testing coffeescript mocha should.js

我正在尝试使用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和节点版本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错误。

3 个答案:

答案 0 :(得分:2)

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

的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)

然后运行(来自proj root):

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

答案 1 :(得分:1)

所以,这完全是奇怪的。非常欢迎解释!这就是我所做的,测试通过了:

 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)

答案 2 :(得分:-1)

使用 CoffeeScript 1.8.0, mocha 1.21.4时,我的测试用例随机

测试用例可以手动批准,但在 mocha 中失败,但有一些奇怪的例外,例如“未定义的函数......”。复制冗长的失败,异常,堆栈跟踪......太简单了......

要解决它:

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