我试图设置一个脚本来执行我的node.js程序的测试,该程序使用MongoDB。这个想法是,我想有一个脚本,我可以运行:对MongoDB数据库中的测试数据运行node.js测试
- 启动MongoDB的过程中,分叉的守护
- 预填充一些测试数据的数据库
- 启动我的节点服务器,直到永远,所以它作为后台进程运行
- 运行我的测试
- 跌落测试从数据库
我有一个执行所有THES粗脚本数据e步骤。我的问题是,MongoDB需要花费不同的时间进行设置,导致在我的脚本中调用sleep
。因此它只是偶尔有效。
# the directory of this script DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # launch mongodb. $DIR/../../db/mongod --fork --logpath ./logs/mongodb.log --logappend --dbpath ./testdb/ --quiet # takes a bit of time for the database to get set up, # and since we make it a daemon process we cant run the tests immediately sleep 1 # avoid EADDRINUSE errors because existing node servers are up. killall node &> /dev/null # start up our node server using a test database. forever start $DIR/../main.js --dbname=testdb --logpath=test/logs/testlog.log # takes a bit of time for node to get set up, # and since we make it a daemon process we cant run the tests immediately sleep 1 # run any database setup code (inject data for testing) $DIR/../../db/mongo 127.0.0.1:27017/testdb $DIR/setup.js --quiet # actually run the tests node $DIR/tests.js # kill the servers (this could be a little less heavy handed...) killall node &> /dev/null killall forever &> /dev/null # finally tear down the database (drop anything we've added to the test db) $DIR/../../db/mongo 127.0.0.1:27017/testdb $DIR/teardown.js --quiet # and then shut mogodb down kill -2 `ps ax | grep mongod | grep -v grep | awk '{print $1}'`
什么是去什么,我试图做的最好方法是什么?我在这里下了一个兔子洞,还是我在MongoDB文档中遗漏了一些东西?
我不知道任何关于节点或mongo,但在其他语言,你会写代码,所以它不会依赖于数据库。 – tzerb
你为什么不睡一会儿?可变时间意味着什么? – mindandmedia
任何原因你不能保持mongodb始终运行?你已经在使用一个测试分贝,所以我不认为你会有冲突的进程。 –