【发布时间】:2017-01-14 04:23:21
【问题描述】:
我正在开发一个 Node.JS 游戏项目,将 Mongoose 用于我的模型并使用 Jasmine 进行测试。每个游戏都有自己的 mongodb 文档,其中包含玩家列表和地图中使用的区域。我的 Mongoose 架构之一需要一种静态方法来搜索具有可用插槽的游戏。如果没有可用的游戏,它将创建一个新游戏并将其返回。如果有可用插槽的游戏,它将返回该游戏。以下是我为此设置的方法:
GameSchema.statics.get_empty_game = function(callback){
//get all games
this.find({players: {$exists: true}, $where:'this.players.length<4'}, function(err, games){
if(err) throw err;
//if there are no games present
if(games.length == 0){
//get all the regions
Region.find({}, function(err, regions){
if(err) throw err;
var regions_docs = [];
//compile region ids in to array of documents
for (var i = 0; i < regions.length; i++) {
regions_docs.push({_id:regions[i]._id, svg_string: regions[i].svg_string});
}
//greate new game with region ids
var game = new this({
regions: regions_docs,
});
//return game
console.log("created new game", game._id);
return callback(game);
});
}else{
console.log("found game with open slots ", game[0]._id);
return callback(game[0]);
}
});
}
这是我设置的测试,用于查看当 DB 中没有游戏时该方法是否会生成游戏:
describe("get_empty_game()", function(){
beforeAll(function(){
Game.remove(function(err, res){
if(err) throw err;
});
});
it("gets game", function(done){
var empty_game = "";
Game.get_empty_game(function(res){
empty_game = res;
});
console.log("empty game: ", empty_game);
expect(empty_game._id).toBeDefined();
expect(empty_game).not.toBe("");
done();
});
});
由于某种原因,这在测试中完全失败了。这是回应:
Started
.empty game:
F
Failures:
1) Game model get_empty_game() gets game
Message:
Expected undefined to be defined.
Stack:
Error: Expected undefined to be defined.
at Object.<anonymous> (/var/www/risk/spec/game-spec.js:40:27)
Message:
Expected '' not to be ''.
Stack:
Error: Expected '' not to be ''.
at Object.<anonymous> (/var/www/risk/spec/game-spec.js:41:27)
2 specs, 1 failure
Finished in 0.013 seconds
我已经用 Jasmine 尝试过各种异步操作,但我通常最终会遇到异步超时错误。我是 Jasmine noobie,所以我想这归结为缺乏经验,但即使在扫描了所有相关的 StackoverFlow 帖子和 Jasmine/Mongoose 文档之后,我似乎仍然无法弄清楚。我希望这不会太模糊,如果您需要更详细的信息,请告诉我。
编辑 #1:响应 Vaterrenanburg 的茉莉花错误:
Failures:
1) Game model get_empty_game() gets new game when NO games are present in collection with less than four players
Message:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
Stack:
Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
at Timer.listOnTimeout [as ontimeout] (timers.js:110:15)
2 specs, 1 failure
Finished in 5.02 seconds
编辑#2: 此处使用console.log时,显示在终端中
GameSchema.statics.get_empty_game = function(callback){
console.log("hello");
//get all games
this.find({players: {$exists: true}, $where:'this.players.length<4'}, function(err, games){
if(err) return callback(err);
但是放在这里的时候却没有出现:
GameSchema.statics.get_empty_game = function(callback){
//get all games
this.find({players: {$exists: true}, $where:'this.players.length<4'}, function(err, games){
console.log("hello");
if(err) return callback(err);
编辑 #3 尝试 $lt 索引后仍然无法正常工作,但我发现了其他有趣的东西。运行以下命令时:
this.find({}, function(err, games){
if(err) return callback(err);
console.log("games:", games);
return callback(games);
});
尽管它是一个简单的查询,不应该花费太长时间来执行并且应该至少返回一个空数组,但我仍然会收到未定义的错误。这可能与 Jasmine 配置有关吗?
【问题讨论】:
-
能否也包括 GameSchema 的定义?这可能是影响您的查询性能的原因。
标签: node.js mongodb mongoose jasmine