Jasmine 的 BeforeAll 内部函数在规范之后调用

Jasmine's BeforeAll inner functions are called after a spec

我正在使用 Phaser-framework 制作游戏,并且正在使用 Jasmine 编写自动测试代码。除了函数 beforeAll() (在 it (spec) 之后调用)之外,此代码中的所有工作正常控制台打印: 测试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');
    });
});

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

尝试从 beforeAll 中返回一个 promise:

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

});