我正在尝试为自定义方法编写mochaoose模型的mocha测试。当我运行我的测试时,它们会失败,因为当我创建一个新模型时它会失败。
这是我的代码:
game.js
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var gameSchema = new Schema({
userName: {type: String, required: true, unique: true},
currentScore: {type: Number, default: 0},
currentFrame: {type: Number, default: 0},
frames: {
type: Array, default: [
{
bowls: [null, null],
score: null
},
{
bowls: [null, null],
score: null
},
{
bowls: [null, null],
score: null
},
{
bowls: [null, null],
score: null
},
{
bowls: [null, null],
score: null
},
{
bowls: [null, null],
score: null
},
{
bowls: [null, null],
score: null
},
{
bowls: [null, null],
score: null
},
{
bowls: [null, null],
score: null
},
{
bowls: [null, null, null],
score: null
}
]
}
});
gameSchema.methods.addBowl = function (count) {
//Ensure the count is an int so that "3" + "7" != 37
count = parseInt(count);
var frame = this.getCurrentFrame();
var index = this._doc.currentFrame;
//Empty frame add the pin count
if (this.isFrameEmpty(index)) {
if (count > 10) {
throw "Cannot bowl more than 10 pins";
}
frame.bowls[0] = count;
}
//First ball bowled and it was not a strike
else if (!this.isFrameEmpty(index) && !this.strike(index) && frame.bowls[1] == null) {
if (parseInt(frame.bowls[0]) + count > 10) {
throw "Cannot bowl more than 10 pins in a frame"
}
frame.bowls[1] = count;
this.nextFrame();
}
//Previous ball was a strike and not in the last frame
else if (this.strike(index) && index < 9) {
index = this.nextFrame();
this.getBowlsInFrame(index)[0] = count;
}
//Spare in the last frame
else if (this.spare(index) && index == 9) {
frame.bowls[2] = count;
}
//Previous ball was a strike and in last frame
else if (this.strike(index) && index == 9 && frame.bowls[1] == null) {
frame.bowls[1] = count;
}
else if (this.strike(index) && index == 9 && frame.bowls[1] != null) {
frame.bowls[2] = count;
} else {
throw "gameSchema over!";
}
}; }
module.exports = mongoose.model('Game', gameSchema);
测试/ game.js
var Game = require('../game');
var expect = require('chai').expect;
describe("Game", function () {
describe("#getFrame", function () {
it("Return the frame with the given index", function () {
var game = new Game();
var indexes = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
indexes.forEach(function (value) {
var frame = game.getFrame(0);
expect(frame).to.have.a.property('bowls');
expect(frame).to.have.a.property('score');
});
expect(function () {
game.getFrame(10)
}).to.throw('Indexing past 10th frame');
})
});
describe('#addBowl', function () {
it("After a strike add the next bowl to the next frame", function () {
var game = new Game();
game.addBowl(10);
game.addBowl(5);
expect(game.currentFrame).to.equal(1);
});
});
describe('#addBowl', function() {
it("Not allow more than 10 pins in a frame", function () {
var game = new Game();
console.log(JSON.stringify(game));
expect(function () {
game.addBowl(11)
}).to.throw("Cannot bowl more than 10 pins");
expect(function () {
game.addBowl(7);
game.addBowl(4);
}).to.throw("Cannot bowl more than 10 pins");
game = {};
});
});
});
我控制台在最后一次测试中登录新游戏时,游戏已经拥有了之前测试的数据。
如何为最后一次测试创建一个没有任何数据的新对象?
答案 0 :(得分:0)
以下是您的代码的精简版本:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var gameSchema = new Schema({
frames: {
type: Array,
default: [{
bowls: [null, null],
score: null
}]
}
});
gameSchema.methods.addBowl = function(count) {
this.frames[0].bowls[0] = 123;
};
var Game = mongoose.model('Game', gameSchema);
var game1 = new Game();
game1.addBowl();
console.log('%j', game1);
var game2 = new Game();
console.log('%j', game2);
如您所见,game2
在frames[0].bowls[0]
中的值与game1
相同。这是因为你的默认声明,它在所有实例中共享。换句话说:您只有[null, null]
个bowls
数组可以在文档之间共享(并且也用于新文档,正如您在代码中看到的那样)。
我认为在Mongoose中设置数组的默认值无论如何都是有问题的,所以我建议你将模式重写为:
var bowlSchema = new Schema({
bowls : [ Number ],
score : Number,
});
var gameSchema = new Schema({
frames: [ bowlSchema ]
});
通过验证或在实例方法中,您可以确保各种阵列保持正确的大小。