成功した場合にステータス コードとパスを含むコールバックを返す非同期関数があります。jasmine を使用して、そのステータス コードを受け取り、今後のすべてのテストにパスを割り当てることをテストしたいと思います。テストで未定義の値を取得し続けます...
function make_valid(cb){
var vdt = child.spawn('node', base,
{cwd: ims_working_dir, stdio: ['pipe', 'pipe', 'pipe']},
function (error, stdout, stderr) {
if (error !== null) {
console.log('spawn error: ' + error);
}
});
vdt.stdout.setEncoding('utf8');
vdt.stdout.on('data', function(data) {});
vdt.on('close', function(code) {
if (code === 0) {
cb(null, '/home/');
} else {
cb(null, code);
}
});
}
describe("IMS", function(cb) {
var path;
jasmine.getEnv().defaultTimeoutInterval = 40000;
beforeEach(function(done, cb) {
console.log('Before Each');
path = make_valid(cb);
done();
});
it("path should not be null", function() {
expect(path).toBeDefined();
});
});
出力は次のとおりです。
Failures:
1) IMS path should not be null
Message:
Expected undefined to be defined.
Stacktrace:
Error: Expected undefined to be defined.
at null.<anonymous> (/home/evang/Dev/dataanalytics/IMS_Tester/spec/ims-spec.js:46:16)
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
Finished in 0.014 seconds
1 test, 1 assertion, 1 failure, 0 skipped
beforeEach 宣言でパスが渡された場合、ファイルを解析するテストをさらに記述したいと思います。コールバックを正しく処理していないか、スパイを使用する必要があると思います。お知らせ下さい!