所以我在我的 express.js 项目中使用 BDD 和 mocha.我才刚刚开始,所以这是我的第一个测试用例:
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'
(这个coffeescript也会生成一些看起来很奇怪的js,因为它会插入返回到最后一条语句.这是用coffeescript设置测试的正确方法吗?)
(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?)
现在当我运行 mocha 时出现此错误:
Now when I run mocha I get this error:
1) Skill #constructor() should return an instance of class skill:
ReferenceError: Skill is not defined
我认为这意味着 Skill.js 没有正确导入.此时我的技能类很简单,只是一个构造函数:
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) ->
如何导入我的模型以便我的 mocha 测试可以访问它们?
How do I import my models so my mocha test can access them?
你需要像这样导出你的 Skill 类:
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'
这篇关于mocha 测试需要外部 js 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!