Created
December 20, 2011 22:20
-
-
Save rajaraodv/1503549 to your computer and use it in GitHub Desktop.
Mocha test for validating emails in an array fails
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
require.paths.unshift('./node_modules'); | |
require('should'); | |
var assert = require('assert'); | |
var mongoose = require('mongoose') | |
, Schema = mongoose.Schema | |
, mongooseTypes = require("mongoose-types") | |
, db = mongoose.createConnection(getMongoUrl()); | |
mongooseTypes.loadTypes(mongoose); | |
var Email = mongoose.SchemaTypes.Email; | |
/* if we use the below code.. we get: CALL_NON_FUNCTION_AS_CONSTRUCTOR exception | |
var emailType = mongoose.SchemaTypes.Email; | |
var Email = new mongoose.Schema({ | |
type: emailType, | |
unique: true, | |
required: true, | |
lowercase: true | |
}); | |
*/ | |
//this test with just 1 email passes | |
describe('Schema with just one email; Simple User with an invalid email', function(){ | |
var UserSchema = new Schema({ | |
email: Email | |
}); | |
var User = db.model('User', UserSchema); | |
//User.remove({}, function (err) {}); | |
describe('Save invalid email', function(){ | |
it('should throw vaidation error', function(done){ | |
var user = new User({email: 'asdf.com'}); | |
user.save(function(err){ | |
err.message.should.equal('Validation failed'); | |
done(); | |
}); | |
}) | |
}) | |
}); | |
//this test with an array of emails fails :( | |
describe('Schema with an ARRAY of emails; User with 1 or more invalid emails', function(){ | |
var UserSchema2 = new Schema({ | |
emails: [Email] | |
}); | |
var User2 = db.model('User2', UserSchema2); | |
describe('Save invalid email', function(){ | |
it('should throw vaidation error', function(done){ | |
var user2 = new User2({emails: ["rrr"]}); | |
user2.save(function(err){ | |
console.log(err); | |
assert.exist(err); | |
assert.exist(err.message); | |
assert.equal(err.message, 'Validation failed'); | |
done(); | |
}); | |
}) | |
}) | |
}); | |
function getMongoUrl() { | |
if (process.env.VCAP_SERVICES) { | |
var env = JSON.parse(process.env.VCAP_SERVICES); | |
var obj = env['mongodb-1.8'][0]['credentials']; | |
} | |
else { | |
var obj = { | |
"hostname": "localhost", | |
"port": 27017, | |
"username": "", | |
"password": "", | |
"name": "", | |
"db": "" | |
} | |
} | |
obj.hostname = (obj.hostname || 'localhost'); | |
obj.port = (obj.port || 27017); | |
obj.db = (obj.db || 'test'); | |
if (obj.username && obj.password) { | |
return "mongodb://" + obj.username + ":" + obj.password + "@" + obj.hostname + ":" + obj.port + "/" + obj.db; | |
} else { | |
return "mongodb://" + obj.hostname + ":" + obj.port + "/" + obj.db; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment