Javascript 如何使用mocha.js测试多个异步流程事件
我尝试用mocha.js测试一些异步流程事件。默认情况下,Javascript 如何使用mocha.js测试多个异步流程事件,javascript,node.js,asynchronous,tdd,mocha.js,Javascript,Node.js,Asynchronous,Tdd,Mocha.js,我尝试用mocha.js测试一些异步流程事件。默认情况下,it方法在done调用后执行同步。使用mocha.js测试多个异步进程的策略是什么 describe('Game', function(done){ var game = new Simulation.Game(); this.timeout(5000); it('should start', function(done){ game.on('start', function() {
it
方法在done
调用后执行同步。使用mocha.js测试多个异步进程的策略是什么
describe('Game', function(done){
var game = new Simulation.Game();
this.timeout(5000);
it('should start', function(done){
game.on('start', function() {
done();
});
});
it('should log', function(done){
game.on('log', function() {
done();
});
});
it('should end', function(done){
game.on('end', function() {
done();
});
});
game.start();
});
您可能需要使用before()钩子来正确设置测试。试试这个:
describe('Game', function(){
var game;
this.timeout(5000);
before(function(before_done) {
game = new Simulation.Game();
game.start();
before_done();
};
it('should start', function(done){
game.on('start', function() {
done();
});
});
it('should log', function(done){
game.on('log', function() {
done();
});
});
it('should end', function(done){
game.on('end', function() {
done();
});
});
});
您可能需要使用before()钩子来正确设置测试。试试这个:
describe('Game', function(){
var game;
this.timeout(5000);
before(function(before_done) {
game = new Simulation.Game();
game.start();
before_done();
};
it('should start', function(done){
game.on('start', function() {
done();
});
});
it('should log', function(done){
game.on('log', function() {
done();
});
});
it('should end', function(done){
game.on('end', function() {
done();
});
});
});
一种方法是使用捕获游戏回调的结果:
describe('Game', function(done){
var started, logged, ended;
// Wrapping the initialization code in a before() block
// allows subsequent describe blocks to be run if an
// exception is thrown.
before(function () {
var game = new Simulation.Game();
var game_on = function (event) {
return new Promise(function (resolve, reject) {
game.on(event, function () {
resolve();
});
});
};
started = game_on('start');
logged = game_on('log');
ended = game_on('end');
game.start();
});
this.timeout(5000);
it('should start', function(){
return started;
});
it('should log', function(){
return logged;
});
it('should end', function(){
return ended;
});
});
game_on函数为调用回调时解决的每个事件创建新的承诺。事件处理程序已正确注册,因为游戏尚未启动
在it区块内,承诺刚刚得到回报。如果他们不解决问题,测试将以超时错误拒绝。一种方法是用来捕获游戏回调的结果:
describe('Game', function(done){
var started, logged, ended;
// Wrapping the initialization code in a before() block
// allows subsequent describe blocks to be run if an
// exception is thrown.
before(function () {
var game = new Simulation.Game();
var game_on = function (event) {
return new Promise(function (resolve, reject) {
game.on(event, function () {
resolve();
});
});
};
started = game_on('start');
logged = game_on('log');
ended = game_on('end');
game.start();
});
this.timeout(5000);
it('should start', function(){
return started;
});
it('should log', function(){
return logged;
});
it('should end', function(){
return ended;
});
});
game_on函数为调用回调时解决的每个事件创建新的承诺。事件处理程序已正确注册,因为游戏尚未启动
在it区块内,承诺刚刚得到回报。如果他们不解决问题,测试将被拒绝,并出现超时错误。这实际上看起来相当不错。我认为你不需要在第一行完成回调。假设游戏对象实际触发了这些事件中的每一个,测试应该通过。要使测试失败,您需要抛出一个错误。您遇到了什么问题?@remyp问题是
.on
上的事件订阅执行得太晚。这样下一个done()
调用就永远不会被触发。如果其中一个事件不触发,测试应该失败。这实际上看起来相当不错。我认为你不需要在第一行完成回调。假设游戏对象实际触发了这些事件中的每一个,测试应该通过。要使测试失败,您需要抛出一个错误。您遇到了什么问题?@remyp问题是.on
上的事件订阅执行得太晚。这样下一个done()
调用就永远不会被触发。如果其中一个事件不会触发,测试应该失败。他应该在之前使用,但这不是他的具体问题。他想知道如何并行运行三个it
函数,而不是顺序运行。他应该在之前使用,但这不是他的具体问题。他想知道如何并行运行三个it
函数,而不是顺序运行。