過去数日間、すでにいくつかの質問を投稿しましたが、それは長すぎました (不可解なフィードバックがなかったので推測しています)。これを簡単にまとめてみました。
次のコードは、「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();
}
});