【发布时间】:2014-04-27 20:57:56
【问题描述】:
过去几天我已经发布了一些问题,这些问题太长了(我猜是因为我没有收到任何非神秘的反馈)。我试图把这个简短。
以下代码使用“setup-complete”事件通知 nodeunit setUp 命令运行测试。测试 1 通过,测试 2 失败
失败:未完成的测试(或其设置/拆卸): - 基于事件的异步代码 - test2
我的代码有错误吗? nodeunit 是测试基于事件的代码的错误选择吗?是我的做法吗? 任何建议表示赞赏。谢谢
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.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();
}
});
【问题讨论】:
-
问题最终是事件侦听器设置在测试之间没有被破坏
标签: javascript node.js unit-testing events nodeunit