需要外部js文件进行摩卡测试 [英] Requiring external js file for mocha testing

查看:180
本文介绍了需要外部js文件进行摩卡测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我和我的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) ->

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

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'

这篇关于需要外部js文件进行摩卡测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆