2014-05-15 38 views
0

刚刚开始接触摩卡并不能为我的生活弄清楚为什么它认为Helper未定义在指定的线下/列:摩卡不能创建我的函数的一个实例

test.js

var assert = require('assert'), 
    helper = require('../src/js/helper.js'); 
describe('helper', function() { 
    describe('#parseValue', function() { 
     it('should return number of minutes for a properly formatted string', function() { 
      assert.equal(1501, (new Helper()).parseValue('1d 1h 1m', 'when')); 
           ^^^^^^^^^^^^ 
     }); 
    }); 
}); 

helper.js

(function(exports) { 

    'use strict'; 

    function Helper(opts) { 

     this.opts = opts || {}; 

     /** 
     * Parse a value based on its type and return a sortable version of the original value 
     * 
     * @param  {string} val  input value 
     * @param  {string} type type of input value 
     * @returns {mixed}  sortable value corresponding to the input value 
     */ 
     this.parseValue = function(val, type) { 

      switch (type) { 

       case 'when': 
        var d = val.match(/\d+(?=d)/), 
         h = val.match(/\d+(?=h)/), 
         m = val.match(/\d+(?=m)/); 
        if (m) 
         m = parseInt(m, 10); 
        if (h) 
         m += parseInt(h, 10) * 60; 
        if (d) 
         m += parseInt(d, 10) * 1440; 
        val = m; 
        break; 

       default: 
        break; 

      } 

      return val; 

     }; 

    } 

    exports.helper = Helper; 

})(this); 

我写了一个快速测试在没有摩卡的浏览器中,以确保我的helper.js功能是可访问的,它工作正常,所以我真的很茫然。我通过从我的目录中的命令行调用mocha直接在我的服务器上运行此操作。

回答

1

你从来没有在test.js -only helper定义Helper在这条线:

var helper = require('../src/js/helper.js'); 

使用小写helper你定义的。


顺便说一句,你可能想从这个在helper.js改变你的出口线路:

exports.helper = Helper; 

要这样:

exports.Helper = Helper; 

然后使用帮手test.js像这样:

assert.equal(1501, (new helper.Helper()).parseValue('1d 1h 1m', 'when')); 

或只是做这样的事情:

var Helper = require('../src/js/helper.js').Helper; 
+0

谢谢。你的编辑很有帮助。 :) – tenub