我試圖設置一個腳本來執行我的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始終運行?你已經在使用一個測試分貝,所以我不認爲你會有衝突的進程。 –