新的 Mongoose 对象在 Mocha 测试期间包含旧数据
New Mongoose object contains old data during Mocha test
我正在尝试为猫鼬模型的自定义方法编写摩卡测试。当我 运行 我的测试失败时,因为当我创建一个新模型时它失败了。
这是我的代码:
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);
test/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 = {};
});
});
});
我在上次测试中控制台记录新游戏的地方游戏已经有以前测试的数据。
如何创建一个没有任何上次测试数据的新对象?
这是您的代码的精简版本:
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 ]
});
通过验证,或在您的实例方法中,您可以确保各种数组保持正确的大小。
我正在尝试为猫鼬模型的自定义方法编写摩卡测试。当我 运行 我的测试失败时,因为当我创建一个新模型时它失败了。
这是我的代码:
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);
test/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 = {};
});
});
});
我在上次测试中控制台记录新游戏的地方游戏已经有以前测试的数据。
如何创建一个没有任何上次测试数据的新对象?
这是您的代码的精简版本:
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 ]
});
通过验证,或在您的实例方法中,您可以确保各种数组保持正确的大小。