Unit testing Coffeescript单元测试在Mocha、Should、Node.js中失败
我正在尝试使用Mocha为CoffeeScript示例类层次结构运行示例CoffeeScript单元测试。但是我不断地犯错误,似乎我无法在没有帮助的情况下修复它们。这是我的示例CoffeeScript文件,位于/src文件夹中:Unit testing Coffeescript单元测试在Mocha、Should、Node.js中失败,unit-testing,coffeescript,mocha.js,should.js,Unit Testing,Coffeescript,Mocha.js,Should.js,我正在尝试使用Mocha为CoffeeScript示例类层次结构运行示例CoffeeScript单元测试。但是我不断地犯错误,似乎我无法在没有帮助的情况下修复它们。这是我的示例CoffeeScript文件,位于/src文件夹中: #Animal.coffee class Animal constructor: (@name) -> move: (meters) -> console.log @name + " moved #{meters}m."
#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\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
但是,由于某些原因,创建对象并保存以供使用仍然失败。我得到了相同的
snake not defined
错误。看起来您有很多问题。请尝试以下操作:
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
测试/动物测试咖啡
should = require "should"
{ Snake } = require "../src/animal"
describe 'sample', ->
it 'should pass', ->
snake = new Snake "Venomous python"
snake.should.be.an.instanceOf(Snake)
然后运行(从项目根目录):
这真是太奇怪了。欢迎解释!这就是我所做的,测试通过了:
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)
当我使用CoffeeScript1.8.0,mocha1.21.4时,测试用例被随机破坏
测试用例可以手动批准,但在摩卡中失败,出现了奇怪的异常,如“未定义的功能…”。太琐碎,无法复制长时间失败、异常、堆栈跟踪
要解决这个问题:
- 乐在其中
- 注释掉所有失败的测试用例
- 尝试直接使用更多javascript
- 尝试使用其他单元测试工具,例如
- 这听起来很愚蠢,但当调试变得糟糕时,一切都发生了,至少对我来说是这样
require
s是相对于当前文件的,而不是相对于项目根目录的(请参见下面的答案),但这不是您唯一的问题…仅列举原始文件的一些问题:(1)您在snake上执行了一个实例检查,但导入了SnakeObj,(2)导入节点模块时不需要执行完整的相对路径,如should
,(3)不知道为什么要使用new snake()
,(4)coffee脚本的编译器应该是coffee脚本而不是coffeescript1)我尝试按原样导入snake,但没有效果。所以接下来我尝试导入SnakeObj,如一个链接的堆栈溢出问题所示。2) 我已尝试按原样导入节点模块,但无法找到should。我已经在全球安装了所有的库,所以我认为这可能是它无法找到它们的原因。3) 这与链接问题中提到的相同。4) 这是复制粘贴,但不知道连字符是如何丢失的。我重新检查了我的命令行上是否有连字符。(1)只要文件中的内容一致,也可以,(2)应该安装在项目的本地(npm install--save should
),(3)链接的问题由提出问题的同一个人回答,看起来就像是他在回答中忘记删除的一行。我做了所有的修改,得到了我上面更新的错误。还安装了npm--save-should,然后运行了这个命令。这很愚蠢,但让我解释一下。我使用升华文本2作为我的编辑器。出于某种原因,它无法理解我使用snake对象所在行上的缩进,尽管在我看来,它与创建它的位置正确对齐。所以,下一个物体落在了范围之外。稍加擦除和重写就成功了。现在它可以工作了!恶心!基于缩进的范围界定!是不是你用了制表符而不是空格(反之亦然)。我不熟悉coffeescript和整个节点环境。所以,我认为这个问题是我不知道的,而帮助我的人一直告诉我这个问题出在哪里。我想,否则的话,这个问题甚至不会出现在这里,我最终会找到答案的。
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
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
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)