2014-05-15 3 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.jshelperHelper을 정의하지 :

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

관련 문제