需要外部js文件进行mocha测试

Mse*_*enb 11 javascript bdd mocha.js node.js coffeescript

所以我正在使用我的express.js项目来玩BDD和mocha.我刚刚开始,所以这里是我的第一个测试用例:

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'
Run Code Online (Sandbox Code Playgroud)

(这个coffeescript也会生成一些奇怪的js,因为它会返回到最后一个语句..这是用coffeescript设置测试的正确方法吗?)

现在,当我运行mocha时,我收到此错误:

 1) Skill #constructor() should return an instance of class skill:
     ReferenceError: Skill is not defined
Run Code Online (Sandbox Code Playgroud)

我认为这意味着skill.js没有正确导入.我的技能类在这一点上非常简单,只是一个构造函数:

class Skill
    constructor: (@name,@years,@width) ->
Run Code Online (Sandbox Code Playgroud)

如何导入模型以便我的mocha测试可以访问它们?

Vad*_*hev 9

你需要像这样导出你的技能类:

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

module.exports = Skill
Run Code Online (Sandbox Code Playgroud)

并将其分配给测试中的变量:

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'
Run Code Online (Sandbox Code Playgroud)