SpookyJS的这种实现确实很诡异。使用Gulp运行Mocha + SpookyJS测试时,我看不到大多数控制台日志输出。我一直在遵循SpookyJS的github page上的快速入门步骤。为什么看不到这些控制台日志输出?
describe('test', function () {
it('test 1', function(done){
try {
var Spooky = require('spooky');
} catch (e) {
var Spooky = require('../lib/spooky');
}
var spooky = new Spooky({
child: {
transport: 'http'
},
casper: {
logLevel: 'debug',
verbose: true
}
}, function (err) {
if (err) {
e = new Error('Failed to initialize SpookyJS');
e.details = err;
throw e;
}
spooky.start(URL);
console.log('Hello 3'); //currently this is not printing anything to the console
spooky.then(function () {
this.emit('hello', 'Hello, from ' + this.evaluate(function () {
return document.title;
}));
});
spooky.run();
console.log('Hello 1'); //currently this is not printing anything to the console
});
spooky.on('hello', function (line) {
console.log(line);
});
spooky.on('console', function (line) {
console.log(line);
});
spooky.on('error', function (e, stack) {
console.error(e);
if (stack) {
console.log(stack);
}
});
console.log('Hello 2'); //this is printing to the console
done();
});
});
最佳答案
简化代码,大致如下所示:
describe('test', function () {
it('test 1', function(done){
var Spooky = require('spooky');
var spooky = create a spooky object with a lot of logic in it;
console.log('Hello 2'); //this is printing to the console
done();
});
});
创建
spooky
对象,然后,程序流程继续。 spooky
对象所做的任何事情,都会在以后异步执行。创建
spooky
对象后,您将得到Hello 2
,然后将调用done()
,从而结束此测试。因此,在可以处理任何
hello
东西之前,您的测试已经结束。可能有帮助的是将
done()
行移到此处:spooky.on('hello', function (line) {
console.log(line);
done();
});
然后,一旦捕获到
hello
事件,您的测试就会结束。由于我对
Spooky
或您要测试的内容并不熟悉,因此恐怕再也无济于事了。希望这可以使您更进一步。