Last active
October 14, 2019 19:43
-
-
Save moyarich/82f080bcfcbb6af9f879ea70a0c88919 to your computer and use it in GitHub Desktop.
mongoose mockgoose test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const assert = require("assert"); | |
const mongoose = require("mongoose"); | |
const Mockgoose = require("mock-mongoose").Mockgoose; | |
mongoose.Promise = global.Promise; | |
mongoose.set("useCreateIndex", true); | |
mongoose.set("debug", false); | |
describe("category connection", function() { | |
const mockgoose = new Mockgoose(mongoose); | |
const uriDB1 = "mongodb://localhost:27017/campaigner_tst1"; | |
let conn1; | |
let SomeModel; | |
const name = "Bob Smith"; | |
// create connection to database | |
before("DB1 connection", async () => { | |
await mockgoose.prepareStorage().then(function() { | |
conn1 = mongoose.createConnection( | |
uriDB1, | |
{ | |
useNewUrlParser: true, | |
useUnifiedTopology: true | |
}, | |
(err, res) => { | |
if (err) throw err; | |
console.log("connected to", uriDB1); | |
} | |
); | |
}); | |
// Define schema | |
const Schema = mongoose.Schema; | |
const someModelSchema = new Schema({ | |
name: String | |
}); | |
// Compile model from schema | |
SomeModel = conn1.model("SomeModel", someModelSchema); | |
}); | |
it("Add data to model", async () => { | |
try { | |
const doc = new SomeModel({ name: name }); | |
doc.save(); | |
assert.equal(doc.name, name); | |
} catch (err) { | |
console.log(`\n\nEncountered error: ${err}`); | |
} | |
}); | |
it("Search for data", async () => { | |
try { | |
const doc = await SomeModel.findOne({ name: name }); | |
assert.equal(doc.name, name); | |
} catch (err) { | |
console.log(`\n\nEncountered error: ${err}`); | |
} | |
}); | |
// remove collections from a temporary store | |
after("Drop db", () => { | |
mockgoose.helper.reset().then(function() { | |
mongoose.connection.close(function() { | |
console.log("Mongoose default connection disconnected"); | |
process.exit(0); | |
}); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment