gpt4 book ai didi

node.js - 使用带有 TDD 接口(interface)的 mocha,ReferenceError

转载 作者:搜寻专家 更新时间:2023-11-01 00:33:37 25 4
gpt4 key购买 nike

我正在尝试通过 Mocha 对 Node 进行单元测试和 Chai .我有点熟悉 Python 内置的 unittest 框架,所以我使用的是 Mocha 的 tdd 接口(interface)和 chai 的 TDD 样式断言。

我遇到的问题是 mocha 的 tdd setup 函数。它正在运行,但我在其中声明的变量在测试中是 undefined

这是我的代码:

test.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

最佳答案

您在其他测试无法访问的范围内声明 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.');
});
});
});

关于node.js - 使用带有 TDD 接口(interface)的 mocha,ReferenceError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15888917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com