简体   繁体   English

使用Mocha和Chai测试NodeJS和MongoDB堆栈时出错

[英]Error while testing NodeJS and MongoDB stack using Mocha and Chai

Right now, I'm running Mocha tests and am getting the following error: 现在,我正在运行Mocha测试,并收到以下错误:

  Error: connect ECONNREFUSED 127.0.0.1:27017
    at Object.exports._errnoException (util.js:873:11)
    at exports._exceptionWithHostPort (util.js:896:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1077:14)

I assume it's because I am unable to connect to port 27017 because I did not include: 我认为这是因为我不包括以下端口而无法连接到端口27017:

var express = require('express')
var app = express()

However, what is particularly confusing to me is how I connect by test to MongoDB so I can create fake records for testing and then destroy them. 但是,令我特别困惑的是如何通过测试连接到MongoDB,以便我可以创建用于测试的伪记录,然后销毁它们。 If anyone can show me (with an example please) how to do it, that would be awesome! 如果有人可以(请举个例子)向我展示如何做,那就太好了!

Thanks again. 再次感谢。

The error is coming may be the mongo server is not running or from more than one server trying to listen on same port. 该错误即将到来可能是mongo服务器未运行或来自多个试图在同一端口上侦听的服务器。 Also for test environment only can create different folder or use different port. 同样对于测试环境,只能创建不同的文件夹或使用不同的端口。 So that can delete the folder once test case is over 这样,一旦测试用例结束,就可以删除该文件夹

In server.js 在server.js中

if(process.env === 'test')
{
    mongoport = 57017;
}
else
{
    mongoport = 27017;
}
mongoUrl = "mongodb://localhost:"+mongoport+"/student"
// use the mongodb url

In test.js 在test.js中

//on start of test case

var fs = require('fs-extra');
fs.removeSync("test/db/");
fs.ensureDirSync("test/db/");
//ur test case definition

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM