使用Phaser 3框架运行Jasmine(单元测试)吗?

时间:2019-04-26 21:30:19

标签: javascript jasmine phaser-framework

这是我的游戏Connect Four代码的摘录。

我要检查和单元测试我使用的一些重要功能。

是否可以在游戏框架Jasmine中使用Phaser 3进行单元测试?每次运行SpecRunner.html时都会失败。 Iam是单元测试中的新功能。 Phaser 3和Jasmine是否不兼容或我的代码错误?

尝试了不同的测试模型和代码结构。

SpecRunner.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Jasmine Spec Runner v3.4.0</title>

  <link rel="shortcut icon" type="image/png" href="lib/jasmine-3.4.0/jasmine_favicon.png">
  <link rel="stylesheet" href="lib/jasmine-3.4.0/jasmine.css">
  <script src="//cdn.jsdelivr.net/npm/phaser@3.16.2/dist/phaser.min.js"></script>

  <script src="../app.js"></script>
  <script src="lib/jasmine-3.4.0/jasmine.js"></script>
  <script src="lib/jasmine-3.4.0/jasmine-html.js"></script>
  <script src="lib/jasmine-3.4.0/boot.js"></script>


  <!-- include source files here... -->
  <!--
  <script src="src/Player.js"></script>
  <script src="src/Song.js"></script>
    -->
  <script src="spec/CheckSpec.js"></script>

  <!-- include spec files here... -->
  <!-- 
  <script src="spec/SpecHelper.js"></script>
  <script src="spec/PlayerSpec.js"></script>
    -->
</head>

<body>
</body>
</html>

app.js

 gameField = [
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [1, 0, 0, 0, 0, 0, 0],
            [1, 0, 0, 0, 0, 0, 0],
            [1, 0, 0, 0, 0, 0, 0],
            [1, 0, 0, 0, 0, 0, 0]
        ];


    function hasOneWonVertical (whoIsPlayer){

        for (var x = 0; x < width; x++){
            for (var y = 5; y >= 3; y--){
                if (
                   (gameField[y][x] == whoIsPlayer) &&
                   (gameField[y-1][x] == whoIsPlayer) &&
                   (gameField[y-2][x] == whoIsPlayer) &&
                   (gameField[y-3][x] == whoIsPlayer)
                   )
                {

                        whoHasWonTheGame = whoIsPlayer;
                        gameOver = true;
                        console.log("HUHU");
                        return whoIsPlayer;
                }
            }
        }
    }

CheckSpec.js

describe("Has player won the game by vertical?", function() {
  it("contains spec with an expectation", function() {
     var resulttt = hasOneWonVertical(1);
        expect(1).toBe(1);
  });
});

运行SpecRunner.html的结果

A suite > contains spec with an expectation
TypeError: Unable to get property '5' of undefined or null reference
    at <Jasmine>
   at hasOneWonVertical (http://127.0.0.1/check-ht/app.js:219:17)
   at Anonymous function (http://127.0.0.1/check-ht/jasmine/spec/CheckSpec.js:3:3)
    at <Jasmine>

0 个答案:

没有答案
相关问题