需要外部js文件进行摩卡testing

所以我和我的express.js项目一起玩BDD和摩卡。 我刚刚开始,所以这是我作为我的第一个testing用例:

should = require "should" require "../lib/models/skill.js" describe 'Skill', -> describe '#constructor()', -> it 'should return an instance of class skill', -> testSkill = new Skill "iOS", "4 years", 100 testSkill.constructor.name.should.equal 'Skill' 

(也这个coffeescript生成一些奇怪的js,因为它插入返回到最后的声明..这是正确的方式来设置一个testing与咖啡脚本?)

现在当我运行摩卡时,我得到这个错误:

  1) Skill #constructor() should return an instance of class skill: ReferenceError: Skill is not defined 

我认为这意味着skill.jsinput不正确。 我的技能类非常简单,只是一个构造函数:

 class Skill constructor: (@name,@years,@width) -> 

如何导入我的模型,以便我的摩卡testing可以访问它们?

你需要像这样导出你的Skill类:

 class Skill constructor: (@name,@years,@width) -> module.exports = Skill 

并将其分配给您的testing中的variables:

 should = require "should" Skill = require "../lib/models/skill.js" describe 'Skill', -> describe '#constructor()', -> it 'should return an instance of class skill', -> testSkill = new Skill "iOS", "4 years", 100 testSkill.constructor.name.should.equal 'Skill' 

如果skill.js位于testing代码的相同path中,请尝试此操作。

 require "./skill.js"