我正在用Phaser框架制作游戏,并在Jasmine中编写自动测试代码。在此代码中,所有功能均正常工作,除了函数beforeAll()(在 it (规范)之后调用),控制台打印: 测试2 测试 什么时候应该打印测试test2。我尝试了beforeEach(),但没有任何区别。
describe("Hit Box", function() {
var collide = false;
beforeAll(function() {
game = new Phaser.Game(400, 400, Phaser.AUTO, '', { preload: preload, create: create, render:render}, false, true);
function preload() {
game.load.image('blue', 'assets/magicien100.png');
game.load.image('fire_ball', 'assets/red.png');
}
function create() {
game.physics.startSystem(Phaser.Physics.ARCADE);
game.world.setBounds(0, 0, 400, 400);
game.dummyPlayer = game.add.sprite(100,100,'blue');
game.dummyPlayer.width = 100;
game.dummyPlayer.height = 100;
game.physics.arcade.enable(game.dummyPlayer);
game.dummySpell = game.add.sprite(50, 50, 'fire_ball');
game.dummySpell.width = 75;
game.dummySpell.height = 75;
game.physics.arcade.enable(game.dummySpell);
game.debug.spriteBounds(game.dummyPlayer);
game.debug.spriteBounds(game.dummySpell);
if (game.physics.arcade.overlap(game.dummyPlayer, game.dummySpell)) {
collide = true;
console.log('test');
}
}
function render(){
game.debug.spriteBounds(game.dummyPlayer);
game.debug.spriteBounds(game.dummySpell);
}
});
it("Should have a collision", function() {
expect(collide).toBe(true);
console.log('test2');
});
});
答案 0 :(得分:0)
如果Phaser.Game是异步的,则运行it函数时它可能仍在创建。
尝试从beforeAll返回承诺:
describe("Hit Box", function() {
var collide = false;
beforeEach(function() {
return new Promise(function(resolve, reject) {
game = new Phaser.Game(400, 400, Phaser.AUTO, '', { preload: preload, create: create, render:render}, false, true);
function preload() {
// preload code
}
function create() {
// create code
resolve(); // to complete the promise
}
});
});