2017-05-08 43 views
0

我是摩卡新手,并试图通过测试我的课程。 我看了几个职位,包括这两个:(初学者)摩卡:如何测试依赖类?

但我无法弄清楚如何使我的作品: -/

我有两类:

Fichier.js

function Fichier(path){ 

    this.local_path = path; 
    this.local_data = NaN; 
} 
module.exports = Fichier 

Arbre.js

function Arbre(chemin) 
{ 
    this.path  = chemin ; 
    this.fichier = new Fichier(this.path); 
    this.noeuds = []; 
    this.fichier.lecture(this.make_structure, [this]);  
} 

ARBRE-spec.js

'use strict'; 

var expect = require('chai').expect ; 
var Fichier = require('./Fichier.js')  ; 

describe('Arbre', function() { 

      it('should exist', function() 
           { 
            var Fichier = require('./Fichier.js') ; 
            var Arbre = require('./Arbre.js' ) ; 
            expect(Arbre).to.not.be.undefined  ; 
           }); 
}); 

的问题是,我的课 “ARBRE” 取决于类 “Fichier”因为测试的输出告诉我:

$ mocha arbre-spec.js 


    Arbre 
hello 
    1) should exist 


    0 passing (9ms) 
    1 failing 

    1) Arbre should exist: 
    ReferenceError: Fichier is not defined 
     at new Arbre (arbre.js:29:22) 
     at Object.<anonymous> (arbre.js:111:16) 
     at require (internal/module.js:12:17) 
     at Context.<anonymous> (arbre-spec.js:13:49) 

我应该如何导入“Fichier”使测试运行?

编辑

继我添加的注释:要求Arbre.js:

require ("./Fichier.js") 
require ("./Noeud.js") 
require ("./Feuille.js") 

我仍然有错误:

//Arbre-spec.js 
'use strict'; 

var expect = require('chai').expect ; 

describe('Arbre', function() 
        { 
         it('should exist', function() 
              { 
               var Arbre = require('./Arbre.js' ) ;       
               expect(Arbre).to.not.be.undefined  ; 
              }); 
         it('', function() 
              { 
               var Arbre = require('./Arbre.js' ) ;                  
               console.log(Arbre) 
               expect(new Arbre("")).to.not.be.undefined  ; 
              }); 
}); 

给我ARBRE内部错误.js功能:

$ mocha Arbre-spec.js 


    Arbre 
    ✓ should exist 
[Function: Arbre] 
    1) 


    1 passing (18ms) 
    1 failing 

    1) Arbre : 
    ReferenceError: Fichier is not defined 
     at new Arbre (Arbre.js:27:22) 
     at Context.<anonymous> (Arbre-spec.js:17:47) 

Arbre.js

require ("./Fichier.js") 
require ("./Noeud.js") 
require ("./Feuille.js") 
function Arbre(chemin) 
{ 
    this.path  = chemin ; 
    this.fichier = new Fichier(this.path); 
    this.noeuds  = []; 
    this.fichier.lecture(this.make_structure, [this]); 


} 
+0

您需要在包含'Arbre'类的文件中需要'Fichier'类。如果您查看堆栈跟踪,您会发现错误发生在您的arbre.js'文件中,而不是在spec文件中。 – djfdev

+0

我改变了相应的问题,但没有更好的结果:-( –

回答

1

您需要分配需要调用为它工作。

Arbre.js例如

var Fichier = require ("./Fichier.js"); 
var Noeud = require ("./Noeud.js"); 
var Feuille = require ("./Feuille.js"); 

function Arbre(chemin) 
{ 
    this.path  = chemin ; 
    this.fichier = new Fichier(this.path); 
    this.noeuds  = []; 
    this.fichier.lecture(this.make_structure, [this]); 


} 

你能想到的modules.exports不具名值使用require你给时(该值可以是一个函数在Fichier.js或别的东西),并通过将其分配给一个变量它是一个名字!否则,程序会崩溃,抱怨你指的是当前范围内不存在的东西。

+0

这应该解决您的问题。对于上下文,您应该阅读更多关于Common.js/Node.js模块加载的信息。这可能与您可能使用过的其他语言不太一样。 – djfdev

+0

添加所有这些“require”后,我如何回到“正常”代码:我的简单http服务器不能识别它们(即使测试通过) –

+0

如果您的JavaScript加载到浏览器页面中,本地支持使用require,当前的书面形式是Node.js中使用的形式。有一些工具可以将它编译成浏览器可读的js。你可以使用webpack(webpack.github.io)。 – axelduch