最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Requiring external js file for mocha testing - Stack Overflow

programmeradmin3浏览0评论

So I'm playing around with BDD and mocha with my express.js project. I'm just getting started so here is what I have as my first test case:

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'

(also this coffeescript generates some odd looking js since it inserts returns to last statement.. is this the correct way to setup a test with coffeescript?)

Now when I run mocha I get this error:

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

Which I assume means skill.js was not imported correctly. My skill class is very simple at this point, just a constructor:

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

How do I import my models so my mocha test can access them?

So I'm playing around with BDD and mocha with my express.js project. I'm just getting started so here is what I have as my first test case:

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'

(also this coffeescript generates some odd looking js since it inserts returns to last statement.. is this the correct way to setup a test with coffeescript?)

Now when I run mocha I get this error:

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

Which I assume means skill.js was not imported correctly. My skill class is very simple at this point, just a constructor:

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

How do I import my models so my mocha test can access them?

Share Improve this question asked Sep 4, 2012 at 7:38 MsencenbMsencenb 5,10411 gold badges55 silver badges86 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 9

You need to export your Skill class like this:

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

module.exports = Skill

And assign it to variable in your test:

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'

if skill.js is in the same path of your test code, try this.

require "./skill.js"
发布评论

评论列表(0)

  1. 暂无评论