我正在使用Mongoose爲我的模型和Jasmine進行測試的Node.JS遊戲項目。每個遊戲都有自己的mongodb文件,其中包含玩家列表和地圖內使用的區域。我的一個Mongoose模式需要一種靜態方法來搜索可用插槽的遊戲。如果沒有遊戲可用,它將創建一個新遊戲並將其返回。如果有可用插槽的遊戲,它將返回該遊戲。以下是方法我設置了這一點:使用茉莉花測試異步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]);
}
});
}
這是我已經設置了看是否該方法將產生一個比賽還有數據庫內沒有遊戲測試:
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
我已經試過各種異步東西茉莉,但我通常最終會得到一個異步超時錯誤。我是一個茉莉花noobie,所以我認爲這是缺乏經驗,但即使掃描每個相關的StackoverFlow文章和茉莉花/貓鼬文檔後,我仍然無法弄清楚。我希望這不是太模糊,如果你需要更詳細地瞭解任何事情,請告訴我。
EDIT#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
EDIT#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);
});
我仍然得到不確定的錯誤,儘管它是不應該花費太長的時間來執行,並應至少返回一個空數組一個簡單的查詢。這可能是與茉莉花配置有關嗎?
請問你還可以包括GameSchema的定義嗎?這可能是影響您查詢性能的因素。 – Vaterrenanburg