Skip to content

Instantly share code, notes, and snippets.

@Neamar
Last active August 29, 2015 14:03
Show Gist options
  • Save Neamar/874eb3165d37be1a7201 to your computer and use it in GitHub Desktop.
Save Neamar/874eb3165d37be1a7201 to your computer and use it in GitHub Desktop.
Failing test case for Circle
"use strict";
// USAGE:
// npm install mongoose
// npm install async
// npm install mocha-mongoose
// node test.js
var mochaMongoose = require('mocha-mongoose');
var mongoose = require('mongoose');
var async = require('async');
// Create a connection to mongo
var mongoUrl = "mongodb://localhost/test";
mongoose.connect(mongoUrl);
// Display all queries
mongoose.set('debug', true);
var UserSchema = new mongoose.Schema({
// User email, with unicity constraint
email: { type: String, unique: true },
});
// Register model
mongoose.model('User', UserSchema);
var U = mongoose.model('User');
async.series([
function clean(cb) {
// Clean from previous tests
mochaMongoose(mongoUrl, {noClear: true})(cb);
},
function first(cb) {
// Create first user, everything should be fine
var u = new U();
u.email = "sad@circle.com";
u.save(cb);
},
function second(cb) {
// Create second user with same email; an err should be raised
var u = new U();
u.email = "sad@circle.com";
u.save(cb);
}
], function(err) {
if(err && err.code === 11000) {
console.log("UNICITY TEST SUCCESS");
}
else {
// No errors raised
console.log("SHOULD NEVER HAPPEN");
}
process.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment