Javascript MochaChai测试奇怪的关闭行为
有人知道在摩卡+柴测试中闭包是如何工作的吗?我不确定测试运行者(在本例中是phantom js)是否把事情搞砸了,但这毫无意义。在每个It功能块中创建的变量似乎相互冲突 基本上,一个游戏只能有10名玩家,但在每个测试中,游戏var中已经有10名玩家。非常混乱Javascript MochaChai测试奇怪的关闭行为,javascript,backbone.js,phantomjs,mocha.js,chai,Javascript,Backbone.js,Phantomjs,Mocha.js,Chai,有人知道在摩卡+柴测试中闭包是如何工作的吗?我不确定测试运行者(在本例中是phantom js)是否把事情搞砸了,但这毫无意义。在每个It功能块中创建的变量似乎相互冲突 基本上,一个游戏只能有10名玩家,但在每个测试中,游戏var中已经有10名玩家。非常混乱 "use strict"; CardsAgainstHumanity.module("Game", function(Game, CardsAgainstHumanity, Backbone, Marionette, $, _){
"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;
});
“严格使用”;
CardsAgainstHumanity.module(“游戏”,函数(游戏,CardsAgainstHumanity,主干,木偶,$,){
Game.Game=Backbone.Model.extend({
最大玩家:10,
默认值:{
玩家:新卡片sagainstumanity.Player.PlayerCollection()
},
addPlayer:函数(播放器){
if(this.get(“players”).size()
我看到的危险信号是:
defaults: {
players: new CardsAgainstHumanity.Player.PlayerCollection()
}
主干模型附加到模型的原型,并被浅层复制到每个实例的属性中。使用这样的默认值
,您创建的模型的每个实例最终将共享完全相同的
。这可以解释为什么您的玩家总是以失败告终我会在你不期望的时候告诉你
对于这类问题,通常的解决方案是为默认值使用函数:
defaults: function() {
return {
players: new CardsAgainstHumanity.Player.PlayerCollection()
};
}
这样,每个实例都将获得自己唯一的默认值对象,从而在其players
属性中获得自己唯一的PlayerCollection
。通常,只要默认值包含可变值(即除数字、字符串和布尔值以外的任何值),您就应该为其使用函数
如果我的猜测是对的,那么你的测试套件就赢了,因为它发现了一个隐藏的、棘手的bug。你从什么证据推断游戏
在你的每个测试中都有10名玩家?在每个测试案例中,当添加任何玩家时,游戏都会抛出一个异常。我在真实的浏览器中运行了它,游戏var有10名玩家第一个测试。一个自定义错误,表示游戏已满。当您尝试添加第11个玩家时,会抛出该错误。我添加了游戏类,以便您可以看到它的功能。目前它几乎没有任何功能。我尝试编写TDD样式的游戏,但效果不太好!从使用摩卡的角度来看,我不知道问题可能是什么。我已经广泛使用摩卡咖啡,但我对主干网不太熟悉。如果你去掉你贴的标签(我建议删除单元测试
),并添加主干网
,作为标签,这样主干网专家就可以看到你的问题了。哦,太好了!我从来没有想过。我回家后会试试的!