๋ฐฉ๊ธ 3.4.0์์ 3.5.1๋ก ์ ๋ฐ์ดํธํ์ผ๋ฉฐ ๋ชจ๋ธ์์ Mocha ํ ์คํธ๋ฅผ '๊ด์ฐฐ'ํ ๋ ํ์ผ์ด ๋ค์ ๋ก๋๋ ๋๋ง๋ค ๋ชจ๋ธ์ ๋ค์ '์๊ตฌ'ํ๋ฉด์ OverwriteModelError๊ฐ ๋ฐ์ํฉ๋๋ค.
'๋ฎ์ด์ฐ๊ธฐ' ์ค๋ฅ๋ฅผ ๋ง๋๋ ๋ฐ ์ฝ๊ฐ์ ๊ฐ์น๊ฐ ์์ด์ผ ํฉ๋๋ค. ํ์ง๋ง ์ด๊ฒ์ด ๋๋ฌด ๊ณ ํต์ค๋ฝ๊ธฐ ๋๋ฌธ์ ์ง๊ธ์ 3.4.0์ผ๋ก ๋์๊ฐ๊ฒ ์ต๋๋ค.
์ด๊ฒ์ ํ๋ก๊ทธ๋๋ฐ ์ค๋ฅ๋ก ์ธํด ๋ฐ์ํฉ๋๋ค. ๊ฐ์ ์ด๋ฆ๊ณผ ์คํค๋ง๋ก mongoose.model()
๋๋ connection.model()
๋ฅผ ์ฌ๋ฌ ๋ฒ ํธ์ถํด๋ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ง ์์ต๋๋ค. ๋ค๋ฅธ ์คํค๋ง๋ก ์กด์ฌํ๋ ๋ชจ๋ธ์ ์ด๋ฆ์ ์ ๋ฌํ๋ฉด ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค. ๋ชจ๋ธ ๋ฎ์ด์ฐ๊ธฐ๋ ์ค์ ๋ก ํ์ฉ๋์ง ์์์ต๋๋ค.
mocha watch๊ฐ ์คํค๋ง๋ฅผ ๋ค์ ์๊ตฌํ๊ณ mongoose.model(preExistingName, newSchema)
๋ฅผ ํธ์ถํ๋ฉด ์ด์ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค. ์ด์ ์ ์ด ์๋๋ฆฌ์ค์์ mongoose๋ ์ ๋ชจ๋ธ์ ์ ํ ์ปดํ์ผํ์ง ์๊ณ ํ๋ก๊ทธ๋๋ฐ ์ค๋ฅ๋ฅผ ์ผํค๊ณ ์ด์ ๋ชจ๋ธ์ ๋ฐํํ๋๋ฐ ์ด๋ ์๋ชป๋ ๋์์
๋๋ค.
var schema = new Schema;
var A = mongoose.model('A', schema);
var B = mongoose.model('A', schema); // no error
var C = mongoose.model('A', new Schema); // previously unreported error
์๊ฒ ์ต๋๋ค. ๊ฐ์ฌ ํด์!
@aheckmann -- ๋๋ ์ด๊ฒ์ ์น๊ณ ์๋๋ฐ ๋ด๊ฐ ๋ญ ์๋ชปํ๊ณ ์๋์ง ์ ๋ชจ๋ฅด๊ฒ ๋ค. ์๋๋ ์ ๊ฐ ์ค์ ํ ๋ฐฉ๋ฒ์
๋๋ค. ์ฒซ ๋ฒ์งธ ๊นจ๋ํ ํ
์คํธ ์คํ์ ๊ด์ฐฎ์ต๋๋ค. ํ์ผ์ ํธ์งํ๊ณ ์ ์ฅํ๋ฉด mocha๊ฐ --watch
๋ก ์ฌ์คํ์ ํธ๋ฆฌ๊ฑฐํ๊ณ ๋ค์์ ์ป์ต๋๋ค.
OverwriteModelError: Cannot overwrite "User" model once compiled.
var mongoose = require('mongoose'),
Schema = mongoose.Schema,
ObjectId = mongoose.SchemaTypes.ObjectId;
var userSchema = new Schema({
name: { type: String }
}),
User;
// other virtual / static methods added to schema
User = mongoose.model('User', userSchema);
module.exports = User;
var User = require('../models/user'),
mongoose = require('mongoose');
mongoose.connection.on('error', function (err) {
console.log(err);
}
mongoose.connection.on('open', function () {
console.log('connected');
}
describe('User', function () {
before(function (done) {
mongoose.connect('mongodb://localhost/test');
done();
}
after(function (done) {
mongoose.disconnect(function () {
console.log('disconnected');
done();
});
});
it('should test something', function (done) {
// tests
done();
});
});
๋ชฝ๊ตฌ์ค ํ ์คํธ์ ๊ฒฝ์ฐ ๋งค๋ฒ ์๋ก์ด ์ฐ๊ฒฐ์ ์์ฑํ๊ณ ์ ์๋ํฉ๋๋ค.
var db = mongoose.createConnection()
https://github.com/LearnBoost/mongoose/blob/master/test/common.js#L74 -L98
Google ๊ทธ๋ฃน์ ๊ฒ์๋ ํ์ ์กฐ์น https://groups.google.com/d/topic/mongoose-orm/PXTjqqpaDFk/discussion
์ ๋ ์ด๊ฑฐ ๋ณด๊ณ ์์ต๋๋ค. ๋ค์์ ์ต์ํ์ ์์ ๋๋ค.
https://github.com/j0ni/mongoose-strangeness
์๋ง๋ ๋ด๊ฐ ๋ชจ๋ธ์ ์ ์ํ๋ ์ฌ๋ฐ๋ฅธ ๋ฐฉ๋ฒ์ ๋์น๊ณ ์๋ ๊ฒ์ผ๊น์...?
https://github.com/j0ni/mongoose-strangeness/blob/master/test.spec.js#L21
var Example = connection.model('Example', ExampleSchema)
์ด์ด์ผ ํฉ๋๋ค.
ํ๋ฅญํ๊ฒ ์๋ํ๋
๊ฐ ํ ์คํธ์ ๋ํด ์ ์ฐ๊ฒฐ์ด ์์ฑ๋๊ณ ๋ชจ๋ธ์ด ๊ฐ๊ฐ์ ๋ํ ์ฐ๊ฒฐ ๋ด์์ ๋ก์ปฌ๋ก ์บ์๋ฉ๋๋ค. ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก ์คํจํ๋ฉด b/c ๋ชจ๋ธ์ด ๊ฐ ํ ์คํธ์ ๋ํด ๋ชฝ๊ตฌ์ค ๋ชจ๋ ์์ค์์ ์ปดํ์ผ๋๋ฏ๋ก ์ถฉ๋์ด ๋ฐ์ํฉ๋๋ค.
@aheckmann - ๊ธฐ๊ด์ฐจ ์ฑ์์ ๋ด ๋ชจ๋ธ์ ํ ์คํธํ๋ ค๊ณ ํ ๋ ์ด ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
๊ธฐ๋ณธ์ ์ผ๋ก ๊ฐ ํ
์คํธ ํ์ผ์ before()
ํจ์์์ Locomotive ์ฑ์ ๋ถํ
ํฉ๋๋ค. ์ฑ์ ๋ถํ
ํ๋ฉด mongo db ์ฐ๊ฒฐ์ด ์์ฑ๋๊ณ ๋ชจ๋ ๋ชจ๋ธ์์ ๋ก๋๋ฉ๋๋ค. ์ด๊ฒ์ ๋ด mongoose ์ด๋์
๋ผ์ด์ ํ์ผ์ ์์ต๋๋ค(์ฑ ์์ ์ ํ ๋ฒ ์คํ).
var mongoose = require("mongoose"),
fs = require("fs");
module.exports = function() {
// Connect to DB
switch (this.env) {
case 'development':
mongoose.connect('mongodb://localhost/slipfeed');
break;
case 'test':
mongoose.connect('mongodb://localhost/slipfeed');
break;
case 'production':
mongoose.connect('mongodb://mongodb.example.com/prod');
break;
}
// Load models
var app = this,
modelPath = app.get('models');
fs.readdirSync(modelPath).forEach(function(file) {
var name = file.substr(0, file.lastIndexOf('.'));
require(modelPath + '/' + name)(mongoose, app);
});
}
๊ฐ ๋ชจ๋ธ ํ์ผ์์ ๊ธฐ๋ณธ์ ์ผ๋ก ๋ค์์ ์ํํฉ๋๋ค.
module.exports = function(mongoose, app) {
var Schema = mongoose.Schema;
var User = new Schema(...);
app.User = mongoose.model('User', User);
});
๊ทธ๋ฆฌ๊ณ ๊ฐ ๋ชจ๋ธ ํ ์คํธ ํ์ผ์์ ๋ค์๊ณผ ๊ฐ์ ์์ ์ ์ํํฉ๋๋ค.
var locomotive = require("locomotive"),
app = new locomotive.Locomotive(),
should = require("should");
describe( "User", function() {
before( function (done) {
app.boot( process.cwd(), 'test', function () {
done();
});
});
after( function (done) {
mongoose.disconnect( function (err) {
if (err) throw err;
console.log('DISCONNECT')
done();
})
});
...tests go here
});
app.boot()
๋ถ๋ถ์ ์๋ฒ๋ฅผ ์์ํ๊ณ ๊ตฌ์ฑ ํ์ผ์ ๋ก๋ํ๊ณ ์ด๊ธฐํ ํ๋ก๊ทธ๋จ(์: db ์ฐ๊ฒฐ ์์๊ณผ ๊ฐ์ ๋ค์ํ ์ฝ๋ ์กฐ๊ฐ์ ํฌํจํ๋ ํ์ผ)์ ํตํด ์คํํฉ๋๋ค.
๊ทธ๋ฌ๋ ์ฒซ ๋ฒ์งธ ํ
์คํธ ํ์ผ์ด ์๋ฃ๋ ํ mocha๊ฐ ๋ค์ ํ์ผ์ ๋ก๋ํ๋ ค๊ณ ์๋ํ๋ฉด OverwriteModelError
๋ฉ๋๋ค.
๋ค์ ํ ์คํธ ํ์ผ์ ๋ก๋ํ ๋ ์ฐ๊ฒฐ์ด ๋ซํ์ง ์๊ฑฐ๋ ์๋ชป๋ ๋ฐฉ์์ผ๋ก ๋ชจ๋ธ์ ์ด๊ธฐํํ๋ ๊ฒ๊ณผ ๊ด๋ จ์ด ์์ ์ ์์์ ์ดํดํฉ๋๋ค.
์ด์จ๋ after()
ํจ์์ mongoose.disconnect
๋ฅผ ์ถ๊ฐํ๋ ค๊ณ ์๋ํ์ง๋ง ๋์์ด ๋์ง ์์์ต๋๋ค.
๊ฐ๋ฐฉ ๋๋ ํ์ ์ฐ๊ฒฐ๊ณผ๋ ์๋ฌด ๊ด๋ จ์ด ์์ต๋๋ค. ๋ค๋ฅธ ์คํค๋ง์ ์ด๋ฏธ ์กด์ฌํ๋ ๋ชจ๋ธ์ ์ฌ์ ์ํ๋ ค๊ณ ํฉ๋๋ค.
var a = new Schema({ x: 'string' });
var b = new Schema({ x: 'string' });
mongoose.model('Thingy', a);
mongoose.model('Thingy', a); // ok, a === the registered schema instance
mongoose.model('Thingy', b); // error a !== the registered schema
FWIW, ์ฝ๊ฐ ๋ ์ฌ์ํ ์์ ์ฝ๋๊ฐ ์ด๊ฒ์ด ๋ฐ๋ณต์ ์ผ๋ก ๋ฐ์ํ๋ ๊ฒ์ ๋ฐฉ์งํ ๊ฒ์ด๋ผ๊ณ ์๊ฐํฉ๋๋ค. ์๋ง๋ ๋จ์ผ ๋ชจ๋ธ์ ์ ์ํ๋ ์์ ์ต์คํ๋ ์ค ์ฑ์ ์ฃผ์ ๊ฐ๋ฅํ ๋ฐฉ์์ผ๋ก ์ฐ๊ฒฐ์ ์ด๊ธฐํํ๊ณ ์ด ๋ฌธ์ ๋ฅผ ์ผ์ผํค์ง ์๊ณ ์ฐ๊ฒฐ์ ์ฃผ์ ํ๋ ๋ชจ์นด ํตํฉ ํ ์คํธ(์ ๊ฒฝ์ฐ์๋ supertest๋ฅผ ์ฌ์ฉํ์ง๋ง ์ค์ ๋ก๋ ๋ฌด์์ด๋ ๊ฐ์)๋ฅผ ์ด๊ธฐํํฉ๋๋ค.
@aheckmann์ ๋ํด ์๊ณ ์๊ณ ์ฐ๋ฆฌ๊ฐ ์ดํด๋ณผ ์ ์๊ณ ํ์ค ํจํด์ ๊ตฌํํ๋ค๊ณ ์๊ฐํ๋ ์ฑ์ด ์๋ค๋ฉด ํธ๋ฆญ์ ์ํํ ๊ฒ์ ๋๋ค.
๋ด๊ฐ ์์ง ํ ๊ฒ์ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
https://github.com/j0ni/beachenergy.ca/blob/master/datamodel/index.js
๋๋ ๊ทธ๊ฒ์ ์ข์ํ์ง ์์ง๋ง ๋ฌธ์ ๋ฅผ ํด๊ฒฐํฉ๋๋ค. ๋ชฝ๊ตฌ์ค ์ํฐ ํจํด์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ(์๋ง๋) ์ ์ ์์ ๊ฐ ๋์์ด ๋ ๊ฒ์ ๋๋ค.
์๋ ํ์ธ์ @aheckmann -- ์ ๋ ๋ค๋ฅธ ์คํค๋ง๋ก ์ ๋ชจ๋ธ์ ๋ง๋ค์ง ์์ ๊ฒ์ด๋ผ๊ณ 100% ํ์ ํฉ๋๋ค. ์ด ๋ฌธ์ ๋ฅผ ์ค๋ช ํ๊ธฐ ์ํด ์ค์ผ๋ ํค/๋ฐ๋ชจ ํ๋ก์ ํธ๋ฅผ ๋ง๋ค์์ต๋๋ค. https://github.com/davisford/mongoose-test
make
mocha๋ฅผ ์์ํ๊ณ ์ฒ์์ผ๋ก ํต๊ณผํ๋ฉด ํ
์คํธ๊ฐ ํต๊ณผํฉ๋๋ค. ์ด์ models/user.js
ํธ์งํ๊ณ ์ ์ฅ(์: CTRL/CMD + S)ํ๋ฉด mocha๊ฐ --watch
๋ณ๊ฒฝ ์ฌํญ์ ์ ํํ๊ณ OverwriteModelError
.
์ฌ๊ธฐ ๋ ๋ค๋ฅธ ์ต์
์ด ์์ต๋๋ค... ์ ๋ nodemon์ ์ฌ์ฉํฉ๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ํ
์คํธ๊ฐ ์คํ๋ ๋๋ง๋ค ํ๋ก์ธ์ค๊ฐ ๋ค์ ์์๋ฉ๋๋ค.
$ nodemon --exec "mocha -R min" ํ
์คํธ
๊ทธ๋ ๋ค๋ฉด ์ด์ ๋ํ ๊ถ์ฅ ์๋ฃจ์ ์ ๋ฌด์์ ๋๊น? ๊ฐ ํ ์คํธ ์ ์ ์ ์ฐ๊ฒฐ์ ๋ง๋์๊ฒ ์ต๋๊น? ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก ์ฌ์์์ Mongoose ๋ชจ๋ธ์ ์ ์ํ๊ณ ์์ฒญํ๋ ๋์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ง ์๋๋ก ํ ์ ์์ต๋๊น?
๋ชจ์นด๋ง์ผ๋ก ํด๊ฒฐํ๋ ค๋ค๊ฐ ํฌ๊ธฐํ์ต๋๋ค. ๋์ ๋ด๊ฐํ๋ ์ผ์ nodemon์ ํตํด mocha๋ฅผ ์คํํ๋ ๊ฒ์ ๋๋ค. ์ด๋ ๊ฒ ํ๋ฉด ๋ ธ๋ ํ๋ก์ธ์ค๊ฐ ๋ค์ ์์๋๊ณ ๋ฌธ์ ๊ฐ ์ ๊ฑฐ๋ฉ๋๋ค.
ํผํฐ ๊ทธ๋ผํ
(๋ด ์์ดํจ๋์์ ๋ณด๋ด์ ์ฒ ์ ์ค๋ฅ๊ฐ ์์ ์ ์์)
+1.512.784.3232
ํผํฐ. [email protected]
2013๋ 2์ 2์ผ ์ค์ 5์ 41๋ถ์ Oliver Joseph Ash [email protected]์์ ๋ค์๊ณผ ๊ฐ์ด ์ผ์ต๋๋ค.
๊ทธ๋ ๋ค๋ฉด ์ด์ ๋ํ ๊ถ์ฅ ์๋ฃจ์ ์ ๋ฌด์์ ๋๊น? ๊ฐ ํ ์คํธ ์ ์ ์ ์ฐ๊ฒฐ์ ๋ง๋์๊ฒ ์ต๋๊น? ๋ค๋ฅธ ๋ฐฉ๋ฒ์ผ๋ก ์ฌ์์์ Mongoose ๋ชจ๋ธ์ ์ ์ํ๊ณ ์์ฒญํ๋ ๋์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ง ์๋๋ก ํ ์ ์์ต๋๊น?
โ
์ด ์ด๋ฉ์ผ์ ์ง์ ๋ต์ฅํ๊ฑฐ๋ GitHub์์ ํ์ธํ์ธ์.
nodemon์ ํตํด mocha๋ฅผ ์ ํํ ์ด๋ป๊ฒ ์คํํฉ๋๊น? ๋๋ ์ด๊ฒ์ ์๋ํ์ง๋ง ์๋ง๋ ๋ฉ๋ฆฌ ๋จ์ด์ ธ์์ ๊ฒ์ ๋๋ค.
nodemon /usr/local/share/npm/bin/mocha --reporter spec --ui bdd --watch ./server/test/*.js
๋๋ ์ฌ์ฉํ๋ค:
nodemon --exec "mocha -R min" ํ
์คํธ
์๋์ ๋ณด์ฌ์ฃผ๋ ์๋ฅผ ๋ณด๋ฉด min์ด ์๋ spec์ ์ฌ์ฉํ๊ณ ์๋ฒ/ํ ์คํธ์ ํ ์คํธ๊ฐ ์๋ ๊ฒ ๊ฐ์ต๋๋ค... ๊ทธ๋ ๋ค๋ฉด ๋งค๊ฐ๋ณ์๋ง ๋ณ๊ฒฝํ๋ฉด ๋ฉ๋๋ค...
2013๋ 2์ 2์ผ ์ค์ 7์์ Oliver Joseph Ash [email protected]์์ ๋ค์๊ณผ ๊ฐ์ด ์ผ์ต๋๋ค.
nodemon์ ํตํด mocha๋ฅผ ์ ํํ ์ด๋ป๊ฒ ์คํํฉ๋๊น? ๋๋ ์ด๊ฒ์ ์๋ํ์ง๋ง ์๋ง๋ ๋ฉ๋ฆฌ ๋จ์ด์ ธ์์ ๊ฒ์ ๋๋ค.
nodemon /usr/local/share/npm/bin/mocha --reporter ์ฌ์ --ui bdd --watch ./server/test/*.js
โ
์ด ์ด๋ฉ์ผ์ ์ง์ ๋ต์ฅํ๊ฑฐ๋ GitHub์์ ํ์ธํ์ธ์.
์ข์๋ฐ. --watch
๋งํผ ๋น ๋ฅด์ง๋ ์์ง๋ง ๋ ์ค์ํ ๊ฒ์ ์๋ํฉ๋๋ค.
๋ํ Mocha ํ ์คํธ์ ํ์ํ ์คํค๋ง๊ฐ ๋ณ๊ฒฝ๋๋ฉด Mocha๊ฐ ์บ์๋ ๋ ๋ค์ ๋ก๋ํ์ง ์๋๋ค๋ ๊ฒ์ ์์์ต๋๋ค.
2013๋ 2์ 2์ผ 15:23์ Peter Graff [email protected]์ด ๋ค์๊ณผ ๊ฐ์ด ์ผ์ต๋๋ค.
๋๋ ์ฌ์ฉํ๋ค:
nodemon --exec "mocha -R min" ํ ์คํธ์๋์ ๋ณด์ฌ์ฃผ๋ ์๋ฅผ ๋ณด๋ฉด min์ด ์๋ spec์ ์ฌ์ฉํ๊ณ ์๋ฒ/ํ ์คํธ์ ํ ์คํธ๊ฐ ์๋ ๊ฒ ๊ฐ์ต๋๋ค... ๊ทธ๋ ๋ค๋ฉด ๋งค๊ฐ๋ณ์๋ง ๋ณ๊ฒฝํ๋ฉด ๋ฉ๋๋ค...
2013๋ 2์ 2์ผ ์ค์ 7์์ Oliver Joseph Ash [email protected]์์ ๋ค์๊ณผ ๊ฐ์ด ์ผ์ต๋๋ค.
nodemon์ ํตํด mocha๋ฅผ ์ ํํ ์ด๋ป๊ฒ ์คํํฉ๋๊น? ๋๋ ์ด๊ฒ์ ์๋ํ์ง๋ง ์๋ง๋ ๋ฉ๋ฆฌ ๋จ์ด์ ธ์์ ๊ฒ์ ๋๋ค.
nodemon /usr/local/share/npm/bin/mocha --reporter ์ฌ์ --ui bdd --watch ./server/test/*.js
โ
์ด ์ด๋ฉ์ผ์ ์ง์ ๋ต์ฅํ๊ฑฐ๋ GitHub์์ ํ์ธํ์ธ์.โ
์ด ์ด๋ฉ์ผ์ ์ง์ ๋ต์ฅํ๊ฑฐ๋ GitHub์์ ํ์ธํ์ธ์.
@j0ni ์ ์๋ฃจ์ ์ ์ ์๊ฒ ํจ๊ณผ์ ์ด์์ต๋๋ค. "mongoose.model"์ ๋ํ ๋ชจ๋ ํธ์ถ์ ๋ค์์ผ๋ก ๋์ฒดํ์ต๋๋ค.
try {
// Throws an error if "Name" hasn't been registered
mongoose.model("Name")
} catch (e) {
mongoose.model("Name", Schema)
}
๋๋ ๊ทธ๋ฌํ ํธ์ถ๋ก ์ฝ๋๋ฅผ ์ด์ง๋ฝํ๋ ๊ฒ์ด ์ข์ง ์๋ค๋ ๋ฐ ๋์ํฉ๋๋ค. ๊ฐ์ฅ ์ผ๋ฐ์ ์ธ ๊ฒฝ์ฐ, ์ฆ ์คํค๋ง๋ฅผ ๋ณ๊ฒฝํ์ง ์๊ณ ๋ฐํ์์ ๋ชจ๋์ด ๋จ์ํ ์ฌ๋ถ์๋๋ ๊ฒฝ์ฐ์ ๋ํด ์ด์ ๋ํ ์ฝ์นญ์ด ์์ผ๋ฉด ์ข์ ๊ฒ์
๋๋ค. mongoose.getOrCreate(name, schema)
์ ๊ฐ์ด ํธ์ถํ ์ ์์ต๋๋ค.
@yourcelf ๋ ๊ทํ์ ์ ์์
module.exports = getOrCreateModel('User', UserSchema);
๋ ๋ค๋ฅธ ์ต์
์ด ์์ต๋๋ค. ๋ชจ๋ ๋ชจ๋ธ๊ณผ ์คํค๋ง์์ ๋ชฝ๊ตฌ์ค๋ฅผ ์ฒญ์ํ๋ ๊ฒ์
๋๋ค.
๋ด ํ
์คํธ ์ฝ๋์์ ๋ค์์ ์ถ๊ฐํ์ต๋๋ค.
mongoose.models = {};
mongoose.modelSchemas = {};
๊ทธ๋ฆฌ๊ณ ๊ทธ๊ฒ์ ์ ์ด๋ ๋๋ฅผ ์ํด ์ ์๋ํฉ๋๋ค.
@remicastaing ์๋ฒฝ, ๋๋ฅผ ์ํด ์๋ํฉ๋๋ค. ๊ฐ์ฌ ํด์!
@remicasting ์ง๊ธ๊น์ง ์ต๊ณ ์ ํด๊ฒฐ ๋ฐฉ๋ฒ!
@remicastaing ๋ํ ๋๋ฅผ ์ํด ์ผํ์ผ๋ฉฐ ์ง๊ธ๊น์ง ์ต๊ณ ์ ์๋ฃจ์ ์ธ ๊ฒ ๊ฐ์ต๋๋ค
@remicastaing ์, ์๋ํ๋ ์๋ฃจ์ ์ฒ๋ผ ๋ณด์ ๋๋ค.
๊ทธ๋ฌ๋ ์ค์ ๋ฌธ์ ๊ฐ ๋ฌด์์ธ์ง ์์ง ๋ชจ๋ฆ ๋๋ค.
@remicastaing ๊ฐ์ฌํฉ๋๋ค, ์ ์๊ฒ๋ ํจ๊ณผ์ ์ ๋๋ค!
@remicastaing ์ ์๊ฒ๋ ์ผํ๊ณ โโ์์ต๋๋ค. ๊ฐ์ฌ ํด์.
์ค๋๋ ๊ฐ๋ ค์์ฆ์ ๊ธ์ด ๋ฏธ์ํ์ง๋ง ๋ชฝ๊ตฌ์ค์ ํจ๊ป mocha -w๋ฅผ ์คํํ๋ ค๊ณ ์๋ํ๋ ๊ฒ๊ณผ ๋์ผํ ๋ฌธ์ ์ ์ง๋ฉดํ๊ณ ์์ผ๋ฉฐ ๊ฐ๋ฅํ ๋ชจ๋ ์๋ฃจ์ ์ ์๋ํ์ง๋ง ์ง๊ธ์ ๋ฒฝ์ ๋ถ๋ชํ๊ณ ์์ต๋๋ค.
์ด๋์ ๋ฃ์ด์ผ ํ๋
mongoose.models = {};
mongoose.modelSchemas = {};
?
์ ๊ฐ, ํ๊ฐ, ํ, ์ ?
beforeEach์์ mongoose.createConnection(), afterEach์์ ๋ซ์์ผ ํ๋์?
๋๋ ๋ฃ์ด
mongoose.models = {};
mongoose.modelSchemas = {};
์งํ
var mongoose = require('mongoose');
config.test.js
ํ์ผ์์ ์ง์ test
ํด๋( config.test.js
ํ
์คํธ ํด๋์ ์ ์ผํ *.test.js
ํ์ผ, ๋ค๋ฅธ *.test.js
ํ์ผ์ ํ์ ํด๋). ๋ชจ์นด๋ ํ
์คํธ ํด๋๋ฅผ ํตํด ์ฌ๊ท์ ์ผ๋ก( --recursive
์ต์
mocha.opts
) config.test.js
ํฉ๋๋ค.
@remicasting ๊ฐ์ฌํฉ๋๋ค! ๋ ์ํด ์ผํด (jasmine)
๋ ๋ค๋ฅธ ๊ฒฝ์ฐ:
์ฝ๋์ ํ
์คํธ์์ ๋์๋ฌธ์๊ฐ ๋ค๋ฅธ ๋ชจ๋ธ์ด ํ์ํ ๋ ์ด ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ต๋๋ค.
์๋ฅผ ๋ค์ด ์ฌ์์์:
var User = require('./models/user');
๊ทธ๋ฆฌ๊ณ ์ฝ๋์์
var User = require('./models/User');
๋ค๋ฅธ ํ์ดํ์ ๋ํ ์บ์๋ ๋ฒ์ ์ด ์๋ค๋ ๊ฒ์ ๋ช
์ฌํ์ญ์์ค.
@asci๊ฐ ๊ฒฝํํ ๊ฒ์ +1. ๋์ผํ ๋ชจ๋ธ์ด ํ์ํ์ง๋ง ๋ ๊ฐ์ ๋ค๋ฅธ ํ์ผ์ ๋ค๋ฅธ ๋์๋ฌธ์๊ฐ ์๊ณ ๋ค๋ฅธ ํ์ผ์ ํ์ํ ๊ฒฝ์ฐ '๋ฎ์ด์ธ ์ ์์' ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
@iandoe ๋๋ mongoose.connect
๋ฅผ ์ฌ์ฉํ๊ณ ๋ชจ์นด ํ
์คํธ๋ฅผ ๋ฃ์ต๋๋ค.
after(function(done){
mongoose.models = {};
mongoose.modelSchemas = {};
mongoose.connection.close();
done();
});
:+1: :ํ:
:+1:
beforeEach
์์ ๋ชจ๋ธ์ ์์ฑํ๋ ค๋ฉด mongoose.model(...);
์์ beforeEach
mongoose.model(...);
์ ๋ฌ๋ฆฌ var db = mongoose.createConnection();
๋ก ์ ์ฐ๊ฒฐ์ ๋ง๋ ๋ค์ db.model(...)
๋ก ์ฐ๊ฒฐํ์ญ์์ค. . ์ด ๋์์ ๋ฒ๊ทธ๊ฐ ์๋๋ผ ์๋์ ์ผ๋ก ์ค๊ณ๋ ๋์์
๋๋ค.
๋ฐํ์์ ๋ค์ ๋ก๋ํ ์ ์๋ ์ฝ๋๊ฐ ์์ผ๋ฏ๋ก ๊ฐ๋ฐ ์ค์ ์คํค๋ง ๋๋ ๋ชจ๋ธ ๊ธฐ๋ฅ์ ๋ณ๊ฒฝํ๋ฉด ๊ธฐ์กด ๋ชจ๋ธ์ ๋ฎ์ด์ฐ๊ณ ์ถ์ต๋๋ค.
์ ํ ์ด์ ๊ฐ ์๋ ํ ์ด๋ฐ ์ผ์ ํ๋ฉด ์ ๋๋ ์ด์ ๋ฅผ ๋ชจ๋ฅด๊ฒ ์ต๋๋ค.
์์
delete mongoose.models['somemodel'];
var somemodel = db.mongoose.model('somemodel', someschema);
์ด๊ฒ์ ๋๋ฅผ ์ํด ์๋ํฉ๋๋ค. ์ ๋ ์ฝ 1๋ ๋์ ๊ทธ๋ ๊ฒ ํด์์ต๋๋ค.
๋๋ ๊ธฐ์กด ๋ชจ๋ธ/์คํค๋ง๋ฅผ ์ง์ฐ์ง ์๊ณ ์คํค๋ง ๋ฐ ๋ชจ๋ธ ๊ธฐ๋ฅ์ ๋ณ๊ฒฝํ๋ ๋ฐฉ๋ฒ์ด ์์ ์ ์์ง๋ง ๋ ์ด์ ์กฐ์ฌํ์ง ์์์ต๋๋ค.
๋ด ์ด ์๋ฃจ์ ์ด ๋์์ด ๋ ์ ์์ต๋๋ค.
@yourcelf ๊ฐ ์ณ์ต๋๋ค.
:+1:
๋๋ ์ด๊ฒ์ด ์ค๋๋์๋ค๋ ๊ฒ์ ์๊ณ ์์ง๋ง ๋๊ตฐ๊ฐ ์ด๊ฒ์ ์ฐ์ฐํ ๋ฐ๊ฒฌํ๋ฉด ๋ค์๊ณผ ๊ฐ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ ์คํธ ์ ํธ๋ฆฌํฐ ํ์ผ์ ๋ง๋ค์ด ํด๊ฒฐํ์ต๋๋ค.
// DatabaseUtils.js
import mongoose from 'mongoose';
const MONGO_URI = 'mongodb://localhost:27017/collectionname'
export function tearDown(){
mongoose.models = {};
mongoose.modelSchemas = {};
resetDb();
if(mongoose.connection.readyState) mongoose.disconnect();
}
export function setUp(){
if(!mongoose.connection.readyState) mongoose.connect(MONGO_URI);
}
export function resetDb(){
if(mongoose.connection.db) mongoose.connection.db.dropDatabase();
}
๊ทธ๋ฐ ๋ค์ ํ ์คํธ ํ์ผ์์ ๋ค์์ ์ฌ์ฉํ ์ ์์ต๋๋ค.
import { setUp, tearDown, resetDb } from './DatabaseUtils'
describe('Testing MongoDB models', function(){
before(setUp)
after(tearDown)
afterEach(resetDb)
it('Some test of a mongoose model', () => {
// Test code here.
}
})
์ด StackOverflow ๋ต๋ณ์ ๋ค์๊ณผ ๊ฐ์ด ํด๊ฒฐํฉ๋๋ค.
let users
try { users = mongoose.model('users') }
catch (e) { users = mongoose.model('users', <UsersSchema...>) }
ํธ์ง : @DanielRamosAcosta๊ฐ ์ง์ ํ ๊ฒ์ฒ๋ผ "๋ฌธ์ ๋ ์คํค๋ง๊ฐ ๋ณ๊ฒฝ๋๋ฉด ๋ณ๊ฒฝ ์ฌํญ์ด ์ ์ฉ๋์ง ์๋๋ค๋ ๊ฒ์ ๋๋ค."
@remicastaing ๊ฐ์ฌ
๊ฐ์ฌํฉ๋๋ค @juanpabloaj , ์ ์๊ฒ ๋์์ด ๋์์ต๋๋ค ๐
@gunar ๋ฌธ์ ๋ ์คํค๋ง๊ฐ ๋ณ๊ฒฝ๋๋ฉด ๋ณ๊ฒฝ ์ฌํญ์ด ์ ์ฉ๋์ง ์๋๋ค๋ ๊ฒ์ ๋๋ค.
@juanpabloaj ๊ณ ๋ง์ ํ!
์ถํ try/catch๋ฅผ ๋ง๋๋ ๋์ ๋ค์์ ์ฌ์ฉํฉ๋๋ค.
let users = mongoose.models.users || mongoose.model('users', <UsersSchema...>)
@tomyam1 , ์๋ง๋ ๊ทธ ํดํค ์๋ฃจ์ ์ด ๋ฌธ์ ์์ด ์ฐ๋ฆฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ณ ์๊ธฐ ๋๋ฌธ์ผ ๊ฒ์ ๋๋ค... :์ค๋ง์ผ:
๋๋ก๋ ํธ๋ฆฌํ ์๋ฃจ์ ์ด ๊ธฐ์ ์ ์ผ๋ก ์ฌ๋ฐ๋ฅธ ์๋ฃจ์ ๋ณด๋ค ๋ ์ ์๋ํฉ๋๋ค. :)
@tomyam1 ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ํ๋ ฅํ๋ ๊ฒ์ ๋ถ๋๋ฌ์ด ์ผ์ด ์๋๋ฉฐ ์ฌ๊ธฐ์ ์๋ฃจ์ ์ ๋ํ ์์ ์ ์๊ฒฌ์ ๊ฒ์ํ ๋ชจ๋ ์ฌ๋์๊ฒ ๊ฐ์ฌํฉ๋๋ค. ์์ธํ ์ค๋ช ํ์ง ์๊ณ - ์ ๊ฒฝ์ฐ์๋ '์๋ชป๋ ํดํค' ์ ์ ์ค ํ๋๊ฐ ์๋ฒฝํ๊ฒ ์๋ํ์ง๋ง '์ฌ๋ฐ๋ฅธ' ์๋ฃจ์ ์ด๋ผ๊ณ ํ๋ ๋ ๊ฐ์ง ๋ชจ๋ ์๋ํ์ง ์์์ต๋๋ค.
์๋ก ๋๋ ๊ฒ์ ๋ถ๋๋ฌ์ด ์ผ์ด ์๋๋๋ค. ๊ฑด์ค์ ์ธ ํฌ์คํ ์ ํด์ฃผ์ ๋ชจ๋ ๋ถ๋ค๊ป ๊ฐ์ฌ๋๋ฆฝ๋๋ค.
๊ทธ๋ผ (garethdown44, fega), ์ฐ๋ฆฌ๋ ๋์ํ์ง ์์ต๋๋ค.
๋ ๋ง์ ์คํธ์ ์์ฑํ์ง ์์ผ๋ ค๋ฉด ์ด๋ชจํฐ์ฝ ๋ฐ์์ ์ฌ์ฉํ์ธ์.
์ด ์ค๋ ๋๋ฅผ ์์ฝํ๋ ค๋ฉด:
์ด ํ์ด์ง์๋ ์ธ ๊ฐ์ง ์๋ฃจ์ ๋ง ์์ต๋๋ค.
mongoose.models.users || mongoose.model('users', <UsersSchema...>)
๋๋
try {
// Throws an error if "Name" hasn't been registered
mongoose.model("Name")
} catch (e) {
mongoose.model("Name", Schema)
}
์ด๊ฒ์ ๋ชจ๋ธ์ ๋ณ๊ฒฝ ์ฌํญ์ ์ ํํ์ง ์๊ธฐ ๋๋ฌธ์ ์๋ฃจ์ ์ด ์๋๋๋ค.
mongoose.models = {};
mongoose.modelSchemas = {};
์ด ์๋ฃจ์
์ ์๋ํ์ง๋ง ์๋ํ์ง ์์์ผ๋ฉฐ ๊ทธ ์ด์ ๋ฅผ ๋๊ฐ ์๋๊น?
๋ฌธ์ํ๋์ง ์์ ๋ด๋ถ ๋ณ์ ์์ ์ ์์กดํฉ๋๋ค.
์๋ง๋ 2013๋
์ ์ ์๋์์ ๋ ์๋ํ์ง๋ง 4๋
ํ์
๋๋ค.
const Mongoose = require('mongoose');
const DB = Mongoose.createConnection(...);
const Model = DB.model("Name", schema);
@tomyam1 - ๋ง์ํ์ ๋ด์ฉ์ ์์น์ ์ผ๋ก ๋์ํฉ๋๋ค. ๊ทธ๋ฌ๋ ๊ณต์ ํ๊ฒ ๊ทธ ์๋ฃจ์ ์ ๋ค๋ฅธ (ํดํค) ์๋ฃจ์ ๋ณด๋ค 2๋ ํ์ ๋์์ต๋๋ค!!
์ ๊ฒฝ์ฐ์๋ nodemon ์๋ฃจ์ ์ ์ ํํ์ต๋๋ค. ๋ฌด์จ ์ผ์ด ์ผ์ด๋๊ณ ์๊ณ ํ ์คํธ ์ฝ๋๊ฐ ์ด๋ป๊ฒ ๊ตฌ์ฑ๋์ด ์๋์ง ๊นจ๋ฌ์์ ๋ ํ ์๊ฐ์ด ์๋ ํฐ ๋ฆฌํฉํ ๋ง์ด ํ์ํ์ ๊ฒ์ ๋๋ค. nodemon ์๋ฃจ์ ์ด ์ ์๋๊ณ ์ฌ๋๋ค์ด +1ํ๊ฒ ๋์ด ๊ธฐ์ฉ๋๋ค. ๊ทธ๊ฒ ๋ค์ผ
์ฌํ๊ฒ๋ Nodemon์ ์ ์ฒด ํ
์คํธ ์ ํ๊ตฐ์ ๋ค์ ์์ํฉ๋๋ค. mocha --watch
์ฌ์ฅ์ ์๋๊ฐ ํจ์ฌ ๋นจ๋ผ์ก์ต๋๋ค.
์ฌํ๊ฒ๋ Nodemon์ ์ ์ฒด ํ ์คํธ ์ ํ๊ตฐ์ ๋ค์ ์์ํ๋ฏ๋ก mocha --watch๊ฐ ํจ์ฌ ๋น ๋ฅด๊ฒ ๋ค์ ๋ก๋๋ฉ๋๋ค.
๋๋ฆฐ ์ฌ๋ก๋ ๋ฐ ์ ์์ ์ธ ๊ฐ๋ฐ์๋ ํ ์คํธ ์คํ์์ ๋ฐ๋ฆฌ์ด(๋๋ ๋ช ์ด)๋ฅผ ์ฌ๋ผ์ด์ฑํ๋ ํธ๋ ์ด๋์คํ์ ๋๋ค.
๋ํ ์ฌ๊ธฐ์ ์ธ๊ธ๋์ง ์์์ผ๋ฏ๋ก npm-watch
๋ ์ด๋ฅผ ์ํํ๋ ๋๊ตฌ(nodemon์ ๋ํ)์ด๋ฏ๋ก ๊ฐ๋จํ npm ์คํฌ๋ฆฝํธ watch
https://www.npmjs๋ฅผ ์ ์ํ ์ ์์ต๋๋ค
๋์ผํ ๋ฌธ์ ๊ฐ ๋ฐ์ํ์ฌ mongoose.modelNames()
๋ฅผ ํ์ธํ๊ณ mocha --watch๊ฐ ์ด ๋ฌธ์ ๋ฅผ ์ผ์ผํค๋ฏ๋ก ๋ชจ๋ธ์ ์ปดํ์ผํ ์ง ์๋๋ฉด ์ด๋ฏธ ์ปดํ์ผ๋ ๋ชจ๋ธ์ ๊ฒ์ํ ์ง ๊ฒฐ์ ํฉ๋๋ค. ์ฝ๋๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
mongoose.modelNames().indexOf("User") === -1 //if my model has not been compiled...
? mongoose.model("User", UserSchema) //...compile model and return it
: mongoose.connection.model("User"); //else it is already compiled, so return this model
์ด์ ์ด๊ฒ์ module.exports
๋ํ ํจ์๋ก ๋ฐํํ๊ณ ("User"๋ฅผ ์ธ์๋ก ๋ฐ๊พธ๊ณ UserSchema๋ฅผ Schema์ ๋ํ ์ธ์๋ก ๋์ฒด) ํ์ํ ๋ ์ด ํจ์๋ฅผ ํธ์ถํฉ๋๋ค.
@remicastaing ์ ๋งค๋ ฅ์ฒ๋ผ ์๋ํฉ๋๋ค. ๊ฐ์ฌํฉ๋๋ค!
์ผ๋ถ ํน์ ์ธ์คํด์คํ์์๋ models
์ฐธ์กฐ๋ฅผ ์ง์์ผ ํ ์ ์์ต๋๋ค.
after((done) => {
Object.keys(mongoose.models).forEach((k) => {
delete mongoose.models[k];
});
Object.keys(mongoose.modelSchemas).forEach((k) => {
delete mongoose.modelSchemas[k];
});
mongoose.connection.close();
done();
});
mocha --watch๋ describe
์ด์ธ์ ๊ฒ์ ์๊ตฌํ์ง ์์ต๋๋ค. ์ฆ, ์คํค๋ง๋ฅผ ์ฌ์ ์ํ์ง ์์ต๋๋ค.
db์ ๋ํ ์ฐ๊ฒฐ ์๋ฅผ ์ต์ํํ๋ ๊ฐ์ฅ ํจ์จ์ ์ด๊ณ ์ฐ์ํ๋ฉฐ ์ดํดํ๊ธฐ ์ฌ์ด ํ ์คํธ ๋ฐฉ๋ฒ์ ํ ์คํธ ์ ํ๊ตฐ ์ธ๋ถ์ ์ฐ๊ฒฐ, ์คํค๋ง ๋ฐ ๋ชจ๋ธ์ ์ค์ ํ๋ ๊ฒ์ ๋๋ค.
์ด ์ฝ๋๋ ๊ฐ ํ ์คํธ ์ ์ ๋ชจ๋ธ์ ์ค์ ํ๋ ๋ค๋ฅธ ์์ ๋น๊ตํ ๋ ๋งค์ฐ DRY์ ๋๋ค(์ ์๊ฒ๋ ํ์ํ์ง ์์).
๋ค์ ํ ์คํธ ๋ชจ์์ด ์๋ํฉ๋๋ค.
const expect = require("chai").expect;
const mongoose = require("mongoose"),
UserSchema = require("../data/models/User");
const connection = mongoose.createConnection(
process.env.MONGO_URL || "mongodb://127.0.0.1/test"
);
const User = connection.model("User", UserSchema);
describe("Database Testing", function() {
it("MongoDB is working and repeatedly testable", function(done) {
let user = User({
username: "My user"
});
user
.save()
.then(doc => {
console.log(doc); // This outputs the doc.
expect(doc.username).to.equal("My user");
done();
})
.catch(err => {
console.error(err);
expect(err).to.be.null;
done();
});
});
});
../data/models/User.js
let mongoose = require("mongoose");
let UserSchema = new mongoose.Schema({
username: String
});
module.exports = UserSchema; //
๋๋ MongoDb๊ฐ ์ฐ๊ฒฐ, ์คํค๋ง ๋ฐ ๋ชจ๋ธ์ ๋ง๋๋ ๋ฐฉ๋ฒ์ ๋ํด ํผ๋์ค๋ฌ์ด ์ํ์ ์์์ต๋๋ค. ๋๋ ๋น์ ์ด ํ๋์ ๋ชจ๋ธ์ ์ ์ํ๊ณ ๊ทธ๊ฒ์ ์ฌ๋ฌ ๋ฒ ์ฌ์ฉํ ์ ์๋ค๋ ๊ฒ์ ๊นจ๋ซ์ง ๋ชปํ์ต๋๋ค (obv. ๊ทธ๋ฌ๋ ...)
์ค๋ช ์๋ฅผ ์ ํํ ๋ฐ๋๊ณ ์คํค๋ง๋ฅผ ์ ์ํ๊ณ ๋ชจ๋ธ์ ๋ฐํํ๋ ๋ชจ๋์ ๋ง๋ค์์ต๋๋ค.
์ฆ, ํ ์คํธ์์ ์คํค๋ง๊ฐ ํ์ํ ๊ฒฝ์ฐ ์คํค๋ง๋ฅผ ์ ์ํ๋ ๋ชจ๋์ด ๊ณ์ ํ์ํ๊ธฐ ๋๋ฌธ์ ์คํค๋ง๊ฐ ์ฌ๋ฌ ๋ฒ ์ฌ์ ์๋ฉ๋๋ค. ๊ทธ๋ฆฌ๊ณ ์คํค๋ง์ ์ฌ๋ฌ โโ์ ์๋ ๋ถ์๋ ธ๊ฐ ์๋๋๋ค.
์ฌ๋๋ค์ ์๋ฃจ์ (๋น๋ก ๋ ํจ์จ์ ์ด์ง๋ง ์๋ํจ)์ ์ฌ์ฉํ๋ฉด ๊ฐ ํ ์คํธ ์ ์ ์ ์ฐ๊ฒฐ์ ๋ง๋ค๊ณ ๊ฐ ํ ์คํธ ์ ์ ์ ๋ชจ๋ธ์ ๋ง๋๋ ๊ฒฝ์ฐ๊ฐ ๋ง์ต๋๋ค.
๊ทธ๋ฌ๋ ๋ค์๊ณผ ๊ฐ์ ๊ฐ๋จํ ์ดํด:
describe
์ด์ธ์ ํญ๋ชฉ์ ๋ ๋ฒ ์ด์ ์คํํ์ง ์์ต๋๋ค.์ด ๋ฌธ์ ๋ฅผ ํจ์จ์ ์ด๊ณ ์ฐ์ํ๊ฒ ํด๊ฒฐํ์ต๋๋ค.
์๋๋ ์ด๊ฒ์ด ์๋ํ๋๋ก ํ๋ ๋ ํจ์จ์ ์ด๊ณ ๋ ์ฐฝ์ ์ธ ๋ฐฉ๋ฒ์ ๋๋ค.
./test/db.spec.js
const expect = require("chai").expect;
const mongoose = require("mongoose"),
// mocha --watch does not rerequire anything
// outside of the test suite ("describe").
// Define your Schemas and models separately, so
// that you can define your Schema once above
// your test suites, and your models many times
// when testing.
UserSchema = require("../data/models/User");
describe("mongoose strangeness", function() {
var connection = mongoose.createConnection(
process.env.MONGO_URL || "mongodb://127.0.0.1/test"
);
// I tried the beforeEach and afterEach, but this wasn't needed:
// beforeEach(function(done) {
// connection = mongoose.createConnection(
// process.env.MONGO_URL || "mongodb://127.0.0.1/test"
// );
// connection.once("open", function() {
// done();
// });
// });
// afterEach(function(done) {
// connection.close(function() {
// done();
// });
// });
it("MongoDB testable", function(done) {
let User = connection.model("User", UserSchema);
let user = User({
username: "My user"
});
user
.save()
.then(doc => {
console.log(doc); // This outputs the doc every time.
expect(doc.username).to.equal("My user");
done();
})
.catch(err => {
console.error(err);
expect(err).to.be.null;
done();
});
});
});
ํ๋ณตํ ๋ชฝ๊ณ ๋ฉ๋น.
์ด ์ฝ๋๋ ์ ์๊ฒ ํจ๊ณผ์ ์ ๋๋ค.
if (mongoose.modelNames().includes('Model')) {
mongoose.deleteModel('Model');
}
mongoose.model('Model', new mongoose.Schema({ ... }));
@verheyenkoen ๋ง์ต๋๋ค. ๋ค์์ deleteModel ๋ฌธ์์ ๋ํ ๋งํฌ์ ๋๋ค. https://mongoosejs.com/docs/api/connection.html#connection_Connection -deleteModel
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋ ๋ค๋ฅธ ์ต์ ์ด ์์ต๋๋ค. ๋ชจ๋ ๋ชจ๋ธ๊ณผ ์คํค๋ง์์ ๋ชฝ๊ตฌ์ค๋ฅผ ์ฒญ์ํ๋ ๊ฒ์ ๋๋ค.
๋ด ํ ์คํธ ์ฝ๋์์ ๋ค์์ ์ถ๊ฐํ์ต๋๋ค.
๊ทธ๋ฆฌ๊ณ ๊ทธ๊ฒ์ ์ ์ด๋ ๋๋ฅผ ์ํด ์ ์๋ํฉ๋๋ค.