Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js Mocha异步测试失败,未定义AssertError_Node.js_Mongodb_Mocha.js - Fatal编程技术网

Node.js Mocha异步测试失败,未定义AssertError

Node.js Mocha异步测试失败,未定义AssertError,node.js,mongodb,mocha.js,Node.js,Mongodb,Mocha.js,我第一次和摩卡一起玩,我很难做一个简单的测试。调用在变量赋值之前返回,因此返回为未定义 下面是我要测试的代码: 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 ema

我第一次和摩卡一起玩,我很难做一个简单的测试。调用在变量赋值之前返回,因此返回为未定义

下面是我要测试的代码:

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();
  });
})
当我运行“摩卡”时,我收到以下信息:

#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)
#emailAddressExistest()
1) 值不存在时应返回1
0通过(10毫秒)
1失败
1) #当值不存在时,EmailAddressExistTest()应返回1:
AssertionError:true==“未定义”
在上下文中。(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
如果没有错误),第二个参数是“返回值”。

helpers.emailAddressExists(returnVal)。它一定是在这里崩溃了,因为
returnVal
没有分配任何值…您的函数
emailAddressExists
没有执行任何您可能想要的操作。您需要查看回调。@MadhavanKumar-它不需要对returnVal进行初始化,并且应该从emailAddressExists调用中为它分配一个值,对吗?@BobAleena您的函数没有执行您希望它执行的操作,以至于它的任何内容都不可测试。它读取数据库,然后丢弃结果;这是禁止的。@BobAleena它不会返回结果。它不会返回任何东西。我想帮你,但不知为什么你不相信我。