2012-09-04 24 views
10

私はexpress.jsプロジェクトでBDDとmochaを使って遊んでいます。私はちょうどので、ここで始めるよ、私は私の最初のテストケースとして持っているものです。モカテストに外部jsファイルを要求する

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を生成。これは、セットアップに正しい方法であります?私は手段のskill.jsを想定

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

が正しくインポートされませんでした:私はモカを実行したときのCoffeeScriptとテスト)

は今、私はこのエラーを取得します。

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

私のスキルクラスは、現時点では非常に単純です。私のモデルをインポートして、それらのモデルにアクセスできるようにするにはどうすればよいですか?

答えて

8

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

module.exports = Skill 

そして、あなたのテストに変数に代入:

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' 
0

skill.jsがテストコードの同じパスにある場合は、これを試してください。

あなたはこのようなあなたのスキルクラスをエクスポートする必要があり
require "./skill.js" 
関連する問題