javascript – 需要外部js文件进行摩卡测试

前端之家收集整理的这篇文章主要介绍了javascript – 需要外部js文件进行摩卡测试前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_404_0@
所以我正在和我的express.js项目一起玩BDD和摩卡车.我刚刚开始,所以这里是我作为我的第一个测试案例:
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设置测试的正确方法?)

现在,当我运行摩卡我得到这个错误

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

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

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

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

解决方法

您需要导出您的技能类,如下所示:
class Skill
    constructor: (@name,@width) ->

module.exports = Skill

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

should = require "should"
Skill = require "../lib/models/skill.js"


describe 'Skill',100
            testSkill.constructor.name.should.equal 'Skill'
原文链接:https://www.f2er.com/js/151814.html

猜你在找的JavaScript相关文章