使用摩卡与TDD接口,ReferenceError

我试图通过摩卡和柴进入unit testing。 我对Python内置的unit testing框架比较熟悉,所以我使用了Mocha的TD界面和chai的TDD风格的断言。

我遇到的问题是摩卡的tdd setupfunction。 它正在运行,但是我在其中声明的variables在testing中是undefined的。

这是我的代码:

test.js

 var assert = require('chai').assert; suite('Testing unit testing === testception', function(){ setup(function(){ // setup function, yu no define variables? // these work if I move them into the individual tests, // but that is not what I want :( var foo = 'bar'; }); suite('Chai tdd style assertions should work', function(){ test('True === True', function(){ var blaz = true; assert.isTrue(blaz,'Blaz is true'); }); }); suite('Chai assertions using setup', function(){ test('Variables declared within setup should be accessible',function(done){ assert.typeOf(foo, 'string', 'foo is a string'); assert.lengthOf(foo, 3, 'foo`s value has a length of 3'); }); }); }); 

其中会产生以下错误:

 ✖ 1 of 2 tests failed: 1) Testing unit testing === testception Chai assertions using setup Variables declared within setup should be accessible: ReferenceError: foo is not defined 

您在其他testing无法访问的范围内声明foo

 suite('Testing unit testing === testception', function(){ var foo; // declare in a scope all the tests can access. setup(function(){ foo = 'bar'; // make changes/instantiations here }); suite('this test will be able to use foo', function(){ test('foo is not undefined', function(){ assert.isDefined(foo, 'foo should have been defined.'); }); }); });