我目前正在使用phaser.js框架开发一个小游戏,由于项目范围相当大,因此我非常想使用单元测试。
然而,在尝试为Phaser设置Jasmine单元测试时,我遇到了有关依赖项的错误。
我没有使用过Jasmine或任何其他测试框架,所以可能是我忽略了一些经验丰富的开发人员显而易见的事情。
我的.spec文件如下:
describe("motorMain", function() {
var Phaser = require('../phaser');
var MotorMain = require("../motorMain");
var motorMain;
var phaser;
beforeEach(function() {
phaser = new Phaser();
motorMain = new motorMain();
});
it("should increase the score if a object is clicked", function(){
var scoreBeforeClicking = motorMain.score;
var gameobject;
motorMain.clickhandler("",gameobject);
expect(scoreBeforeClicking+1).toEqual(score);
})
});
但是由于Phaser依赖于在浏览器中运行,当我运行它时,它会抱怨无法访问诸如
window
和document
等元素。我遇到了以下错误:
有人有测试Phaser游戏的经验吗?我似乎找不到任何在线信息。测试Phaser游戏是否可能?ReferenceError: document is not defined
至于其他测试框架,我也研究了nightwatch,但它的Phaser支持已过时,而且主要是端到端测试而不是单元测试,所以不符合我的需求。我还发现网上有人开发了一个Phaser 2.4.7的shimming版本,但这已经过时了,因为Phaser 3已经发生了很多变化。