我正在尝试通过Mocha和Chai使用节点进行单元测试。我对 Python 的内置unittest
框架有点熟悉,所以我使用的是 Mocha 的 tdd 接口和 chai 的 TDD 样式断言。
我遇到的问题是 mocha 的 tddsetup
函数。它正在运行,但我在其中声明的变量undefined
在测试中。
这是我的代码:
测试.js
var assert = require('chai').assert;
suite('Testing unit testing === testception', function(){
setup(function(){
// setup function, y u 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