Last active
September 9, 2019 13:18
-
-
Save nkzawa/4971592 to your computer and use it in GitHub Desktop.
Testing mongoose models with mocha
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
language: node_js | |
node_js: | |
- 0.8 | |
services: | |
- mongodb |
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
mongoose = require 'mongoose' | |
mongoose.connect 'mongodb://localhost/test' | |
connection = mongoose.connection | |
before (done) -> | |
connection.on 'open', -> | |
connection.db.dropDatabase done | |
after (done) -> | |
connection.close done | |
module.exports = -> | |
afterEach (done) -> | |
connection.db.dropDatabase done | |
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
{ | |
"name": "testing-mongoose-models-with-mocha", | |
"version": "1.0.0", | |
"scripts": { | |
"test": "PATH=./node_modules/.bin:$PATH; mocha --compilers coffee:coffee-script src" | |
}, | |
"dependencies": { | |
"coffee-script": "1.4.0", | |
"mongoose": "3.5.6" | |
}, | |
"devDependencies": { | |
"mocha": "1.8.1", | |
"chai": "1.5.0" | |
} | |
} | |
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
{expect} = require 'chai' | |
db = require './db' | |
User = require './user' | |
describe 'User', -> | |
# call when testing db | |
db() | |
describe '#save', -> | |
it 'should save user', (done) -> | |
data = {email: '[email protected]'} | |
user = new User data | |
user.save (err, user) -> | |
done err if err | |
expect(user.email).to.equal data.email | |
expect(user.created).to.exist | |
done() | |
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
mongoose = require 'mongoose' | |
schema = new mongoose.Schema | |
email: String | |
created: {type: Date, default: Date.now} | |
module.exports = mongoose.model 'User', schema | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is it unfinished?