Jasmine's Before所有内部函数都是在规范之后调用的



我正在使用Phaser框架制作游戏,并且正在用Jasmine编写自动测试代码。在此代码中一切正常,在 All(( 之前执行函数(在it(spec(之后调用( 控制台打印: 测试2 测试 何时应打印测试测试2。我尝试了之前的每个((,但它没有任何区别。

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');
});
});

如果 Phaser.Game 是异步的,则在运行 it 函数时它可能仍在创建。

尝试从之前返回承诺所有:

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
}
});

}(;

最新更新