試圖測試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