试图测试JavaScript对象如何测试一个Javascript对象属性时摩卡功能,柴
Game.js
var GameManager= {
gameType: 'room',
roomDimension: [10,10],
playerDirection: ["W"]
possibleDirections: ["N","E","S","W"],
init: function(){
if(this.gameType == 'room'){
regexpNumber = /^-?[0-9]$|-?([1][0-9])$/;
}
return true;
},
turnRight : function(){
var movePosition = this.possibleDirections.indexOf(this.playerDirection);
if(movePosition == this.possibleDirections.length -1){
return this.possibleDirections[0];
}
return this.possibleDirections[movePosition + 1];
},
commandString: function(string){
var command = /^[PQR]*$/;
if(command.test(string){
return true;
}
this.errorMessageNumber = 0;
return false;
}
}
这里是我的测试脚本test.js
var expect = require("chai").expect;
var GameManager = require("../GameManager.js");
describe("Test game type", function() {
beforeEach(function() {
GameManager.gameType = 'room';
GameManager.roomDimension= [10, 10];
GameManager.possibleDirections: ["N","E","S","W"];
});
it('should commandString be as parameters', function() {
expect(GameManager.commandString("AABB")).to.not.be.false;
});
it('should init toBeTruthy', function() {
expect(GameManager.init()).ok;
});
});
问题:在两种情况下,测试失败,出现TypeError
错误,如下所示,其中一个t他测试:
1) Test game type should init toBeTruthy:
TypeError: GameManager.init is not a function at Context.<anonymous>
由于init
在这里不被认为是功能,所以如何测试?
测试的预期看起来不错,也许'常量期待=需要('柴')期望;'你进口柴的方式。 – Hosar
另外你是如何导出GameManager的? – Hosar
@Hosar将代码更改为'var expect = require(“chai”)。expect;' 我不输出GameManager,因为它是一个简单的Javascript对象。不使用“Typescript”导出类。 – shaz