I am playing around with Mocha for the first time and I'm having difficulty to get a simple test to work. The call returns before the variable has been assigned and thus comes back as undefined.
Here is the code I would like to test:
var mongodb = require('mongodb')
var querystring = require("querystring");
var mongoURI = process.env.MONGOLAB_URI;
var dbName = process.env.dbName;
//checks for a single email address
var emailAddressExists = function () {
var returnVal;
mongodb.connect(mongoURI, function (err, db) {
if (err)
{ console.error(err); response.send("Error " + err); }
var collection = db.collection(dbName); //, function(err, collection) {
collection.find( { "emailAddress" : "myemail@email.com"} ).count( function (err, count) {
if (count == 0) {
returnVal = false;
console.log("not Matched " + returnVal);
} else {
returnVal = true;
console.log("matched " + returnVal);
}
return returnVal;
});
});
)
exports.emailAddressExists = emailAddressExists;
The test I have is:
var assert = require('assert'),
helpers = require ('../lib/helpers.js');
describe('#emailAddressExistsTest()', function() {
var returnVal;
it('should return 1 when the value is not present', function(done) {
assert.equal(true, helpers.emailAddressExists(););
done();
});
})
When I run 'mocha' I receive the following:
#emailAddressExistsTest()
1) should return 1 when the value is not present
0 passing (10ms)
1 failing
1) #emailAddressExistsTest() should return 1 when the value is not present:
AssertionError: true == "undefined"
at Context.<anonymous> (test/emailAddressCheck.js:25:11)