Mocha 异步测试因未定义的 AssertError 而失败
Mocha async test failing with undefined AssertError
我是第一次玩摩卡,我很难让一个简单的测试工作。在分配变量之前调用 returns,因此返回为未定义。
这是我要测试的代码:
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;
我的测试是:
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();
});
})
当我 运行 'mocha' 我收到以下信息:
#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)
首先,您将要更改 emailAddressExists
以进行回调 - 这是在完成时告诉测试的唯一方法:
var emailAddressExists = function (next) {
mongodb.connect(mongoURI, function (err, db) {
if (err) {
next(err);
}
var collection = db.collection(dbName);
collection.find( { "emailAddress" : "myemail@email.com"} ).count( function (err, count) {
if (count == 0) {
next(null, false);
} else {
next(null, true);
}
return returnVal;
});
});
)
然后,你必须给它传递一个回调并在回调中调用done
:
describe('#emailAddressExistsTest()', function() {
it('should return 1 when the value is not present', function(done) {
helpers.emailAddressExists(function(err, returnVal) {
// Maybe do something about `err`?
assert.equal(true, returnVal);
done();
});
});
})
您会注意到这与我们在聊天中讨论的内容有些不同。 node.js
中的约定是回调的第一个参数是错误(null
如果没有错误),第二个参数是 "return value".
我是第一次玩摩卡,我很难让一个简单的测试工作。在分配变量之前调用 returns,因此返回为未定义。
这是我要测试的代码:
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;
我的测试是:
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();
});
})
当我 运行 'mocha' 我收到以下信息:
#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)
首先,您将要更改 emailAddressExists
以进行回调 - 这是在完成时告诉测试的唯一方法:
var emailAddressExists = function (next) {
mongodb.connect(mongoURI, function (err, db) {
if (err) {
next(err);
}
var collection = db.collection(dbName);
collection.find( { "emailAddress" : "myemail@email.com"} ).count( function (err, count) {
if (count == 0) {
next(null, false);
} else {
next(null, true);
}
return returnVal;
});
});
)
然后,你必须给它传递一个回调并在回调中调用done
:
describe('#emailAddressExistsTest()', function() {
it('should return 1 when the value is not present', function(done) {
helpers.emailAddressExists(function(err, returnVal) {
// Maybe do something about `err`?
assert.equal(true, returnVal);
done();
});
});
})
您会注意到这与我们在聊天中讨论的内容有些不同。 node.js
中的约定是回调的第一个参数是错误(null
如果没有错误),第二个参数是 "return value".