有谁知道闭包在 Mocha + Chai 测试中是如何工作的?我不确定测试运行器(在这种情况下为 phantom-js)是否搞砸了,但这没有任何意义。看起来在每个 it 功能块中创建的 var 会相互冲突。
基本上一个游戏只能有 10 名玩家,但在每次测试中,游戏变量已经有 10 名玩家。很混乱。
"use strict";
CardsAgainstHumanity.module("Game", function(Game, CardsAgainstHumanity, Backbone, Marionette, $, _){
Game.Game = Backbone.Model.extend({
maxPlayers: 10,
defaults: {
players: new CardsAgainstHumanity.Player.PlayerCollection()
},
addPlayer: function(player){
if(this.get("players").size() < this.maxPlayers){
this.get("players").add(player);
}
else{
throw(Error("This game is currently full"));
}
},
removePlayer: function(player){
this.get("players").remove(player);
}
});
});
describe.only("players can be added and removed", function(){
it("should add a player if there is space", function(){
var game = new CardsAgainstHumanity.Game.Game();
var player = new CardsAgainstHumanity.Player.Player({
id: 1
});
game.addPlayer(player);
game.get("players").contains(player).should.be.true;
});
it("should not add a player if the game is full", function(){
var game = new CardsAgainstHumanity.Game.Game();
_.times(game.maxPlayers, function(index){
var game = new CardsAgainstHumanity.Game.Game();
game.addPlayer(new CardsAgainstHumanity.Player.Player({
id: index
}));
});
(function(){
game.addPlayer(new CardsAgainstHumanity.Player.Player({
id: game.maxPlayers
}));
}).should.throw(Error("This game is currently full"));
});
it("should remove said player if said player is found", function(){
var game = new CardsAgainstHumanity.Game.Game();
var player = new CardsAgainstHumanity.Player.Player({
id: 1
});
game.addPlayer(player);
game.get("players").contains(player).should.be.true;
game.removePlayer(player);
game.get("players").contains(player).should.be.false;
});