我是摩卡新手,并试图通过测试我的课程。 我看了几个职位,包括这两个:(初学者)摩卡:如何测试依赖类?
但我无法弄清楚如何使我的作品: -/
我有两类:
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]);
}
您需要在包含'Arbre'类的文件中需要'Fichier'类。如果您查看堆栈跟踪,您会发现错误发生在您的arbre.js'文件中,而不是在spec文件中。 – djfdev
我改变了相应的问题,但没有更好的结果:-( –