基于Nodeunit测试事件的异步代码 [英] Nodeunit testing event based async code
问题描述
过去几天我已经发布了几个问题,太长了(我猜,因为我没有收到任何非隐私的反馈)。以下代码使用setup-complete事件来通知nodeunit setUp命令来运行测试。测试1通过,测试2失败与
失败:未测试(或其设置/拆除):
- 基于事件的异步代码 - test2
我的代码有一些错误吗? nodeunit是测试基于事件代码的不好选择吗?我的做法
任何建议赞赏。感谢
async_setup.js:
var
事件= require('events'),
setup = new events.EventEmitter;
module.exports = function(app,cb){
setup.on('setup-complete',function(){
cb();
} );
setTimeout(function(){
if(app.result)throw new Error(AlreadyConfiguredAppError);
app.result =app is configured;
setup.emit ('setup-complete',app.result);
},5000);
return app;
};
test / test.js:
var
nodeunit = require('nodeunit'),
async_setup = require('../async_setup');
导出['基于事件的异步代码'] = nodeunit.testCase({
setUp:function(callback){
this.app = {};
async_setup (this.app,callback);
},
tearDown:function(callback){
delete this.app;
callback();
},
'test1':function(t){
t.expect(1);
t.ok(this.app.result!== undefined,'app配置');
t.done();
},
'test2':function(t){
t.expect(1);
t.ok(this.app.result!== undefined,'app is configured');
t.done();
}
});
问题最终是事件侦听器设置测试之间不会被破坏。我通过修改setUp函数来修复问题,以使用proxyquire与noPreserveCache标志设置:
var proxyquire = require('proxyquire' );
...
setUp:function(callback){
this.app = {};
proxyquire.noPreserveCache();
var async_setup = proxyquire('../async_setup',{});
async_setup(this.app,callback);
}
Webstorm中的错误消息有更多的信息,堆栈跟踪到nodeunit异步模块错误:
iterator(x.value,function(err,v){
无法读取属性值'未定义的
当我浏览代码时,我注意到有两个事件侦听器设置,即使我
希望这有助于别人在那里。
I've already posted a few question the past few days, which were too long (I'm guessing because I didn't receive any non cryptic feedback). I've tried to make this brief.
The following code uses a 'setup-complete' event to notify the nodeunit setUp command to run the tests. Test 1 passes, test 2 fails with
FAILURES: Undone tests (or their setups/teardowns): - event based async code - test2
Is there some mistake in my code? Is nodeunit a bad choice for testing event based code? Is my approach? Any advice appreciated. thanks
async_setup.js:
var
events = require( 'events' ),
setup = new events.EventEmitter;
module.exports = function ( app, cb ) {
setup.on( 'setup-complete', function () {
cb();
});
setTimeout( function () {
if ( app.result ) throw new Error( "AlreadyConfiguredAppError" );
app.result = "app is configured";
setup.emit( 'setup-complete', app.result );
}, 5000 );
return app;
};
test/test.js:
var
nodeunit = require( 'nodeunit' ),
async_setup = require( '../async_setup' );
exports[ 'event based async code' ] = nodeunit.testCase({
setUp: function ( callback ) {
this.app = {};
async_setup( this.app, callback );
},
tearDown: function ( callback ) {
delete this.app;
callback();
},
'test1': function ( t ) {
t.expect( 1 );
t.ok( this.app.result !== undefined, 'app is configured' );
t.done();
},
'test2': function ( t ) {
t.expect( 1 );
t.ok( this.app.result !== undefined, 'app is configured' );
t.done();
}
});
The problem ended up being that the event listeners setup weren't being destroyed between tests. I fixed the issue by modifying the setUp function to use proxyquire with the noPreserveCache flag set:
var proxyquire = require( 'proxyquire' );
...
setUp: function ( callback ) {
this.app = {};
proxyquire.noPreserveCache();
var async_setup = proxyquire( '../async_setup', {} );
async_setup( this.app, callback );
}
The error message in Webstorm had more info, with a stack trace to a nodeunit async module error:
iterator(x.value, function (err, v) {
Cannot read property 'value' of undefined
When I stepped through the code I noticed that there were two event listeners setup even though I only set one in the test.
Hope this helps someone else out there.
这篇关于基于Nodeunit测试事件的异步代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!