我有一个异步函数,如果成功,它会返回一个带有状态代码和路径的回调。我想使用 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 声明中传递的路径,我想在解析文件的地方编写更多测试。我认为我正在错误地处理回调,或者我可能需要使用间谍。让我知道!