简体   繁体   English

Coffeescript单元测试因Mocha,Should,Node.js失败

[英]Coffeescript unit test failing with Mocha, Should, Node.js

I am trying to run a sample CoffeeScript unit test for a CoffeeScript sample class hierarchy, using Mocha. 我正在尝试使用Mocha为CoffeeScript示例类层次结构运行示例CoffeeScript单元测试。 But I keep getting errors and it doesn't seem I can fix them without some help. 但是我一直在收到错误,而且似乎在没有任何帮助的情况下也无法修复它们。 This is my sample CoffeeScript file, in /src folder : 这是我的示例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

And this is the CoffeeScript Unit Test in /Tests folder : 这是/ 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)

I have installed all these libraries globally. 我已经在全球范围内安装了所有这些库。 So when I execute this command through command line (Windows 7) : 因此,当我通过命令行(Windows 7)执行此命令时:

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

It throws me this error: 它抛出了这个错误:

    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

I am using CoffeeScript version 1.7.1, Mocha version 1.18.2 and Node version 0.10.26. 我正在使用CoffeeScript版本1.7.1,Mocha版本1.18.2和Node版本0.10.26。 I have tried looking everywhere and tried all combinations between the tests, but I cannot figure out for my life how to make it create the objects successfully and run the test. 我尝试到处寻找并尝试了测试之间的所有组合,但是我一生都无法想出如何使它成功创建对象并运行测试的方法。

For reference, I tried these, of all other combinations : Requiring CoffeeScript file from within another CoffeeScript file with nodejs 作为参考,我尝试了所有其他组合的这些方法: 使用nodejs从另一个CoffeeScript文件中请求CoffeeScript文件

How to check for class inheritance in Coffeescript Mocha Test? 如何在Coffeescript Mocha Test中检查类继承?

Can someone point out what could possibly be wrong? 有人可以指出可能出什么问题吗?

Update 1: Modified everything as pointed by dule, but now I get this error: 更新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

Update 2: I made the following changes to the AnimalTest script and the test passed. 更新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")

However, for some reason, creating an object and saving it for use, still fails. 但是,由于某种原因,创建对象并将其保存以供使用仍然失败。 And I get the same snake not defined error. 和我得到相同的snake not defined错误。

Looks like you have a number of issues. 看来您有很多问题。 Try the following: 请尝试以下操作:

src/animal.coffee: 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

tests/animalTest.coffee 测试/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)

Then run (from proj root): 然后运行(从项目根目录开始):

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

So, this is totally weird. 因此,这完全是奇怪的。 An explanation is more than welcome! 一个解释是非常受欢迎的! This is what I did and the test passed : 这就是我所做的,测试通过了:

 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)

I got test case broken randomly when using CoffeeScript 1.8.0, mocha 1.21.4. 使用CoffeeScript 1.8.0和Mocha 1.21.4时,我的测试用例随机中断。

The test case can be approved by hand, but it failed in mocha with strange exceptions like "undefined function...". 可以手动批准该测试用例,但是它在mocha中失败,并带有诸如“ undefined function ...”之类的奇怪异常。 Too trivial to copy the long long fails, exceptions, stack traces... 无法复制长久失败,异常,堆栈跟踪...

To solve it: 解决方法:

  • Enjoy the trouble 享受麻烦
  • Commented out all failed testing cases 注释掉所有失败的测试案例
  • Try to use more javascript directly 尝试直接使用更多JavaScript
  • Try to use other unit test tools, such as nodeunit 尝试使用其他单元测试工具,例如nodeunit
  • It sounds stupid, but when debugging becomes terrible, all happens , at least to me. 这听起来很愚蠢,但是当调试变得很糟糕时, 一切都会发生 ,至少对我而言。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Mocha,Chai和Sinon对Node.js应用程序进行单元测试 - Unit Test a Node.js application with Mocha, Chai, and Sinon 单元测试 node.js mongoose mocha chai sinon - unit test node.js mongoose mocha chai sinon 在 Mocha 单元测试 Node.js 期间找不到环境变量 - Environment variables not found during Mocha unit test Node.js 如何使用Mocha单元测试node.js功能? - How can I unit test node.js functions with Mocha? Node.js / Javascript:在将XML解析为JSON的测试中,mocha测试失败并超时 - Node.js/Javascript: Failing mocha test with timeout on testing parsing XML to JSON 使用 mocha 在 node.js 中编写单元测试用例来模拟 Azure 服务总线队列以接收消息 - Writing a unit test case in node.js using mocha to mock a azure service bus queue to recive messages 使用 mocha 在 node.js 中编写单元测试用例来模拟 azure 服务总线队列 - Writing a unit test case in node.js using mocha to mock a azure service bus queue 在Mocha Javascript Node.js单元测试框架中未收到来自chai .post请求的响应 - Not getting a response from a chai .post request in mocha javascript node.js unit test framework 如何使用Mocha和Chai单元测试此node.js模块? - How can I unit test this node.js module using mocha and chai? Node.js Istanbul / Mocha单元测试未打印详细信息 - Node.js Istanbul/Mocha Unit Tests not printing details
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM