Using a testing database with Restify

╄→尐↘猪︶ㄣ 提交于 2019-12-11 11:05:25

问题


I created a simple Restify server, and started testing its functionality through Mocha using its own JSONclient.

When Unit Testing functionality, it's possible to set an ENV var indicating a testing setup, and connect to the according mongodb database.

However, when using the JSONClient you, obviously, test the 'running' API server, which is already connected.

Is there any way to end-to-end test API functionality through the client by swithing database-connections as to not overwrite the development database?

Edit: I suppose I could add a method to the api along the lines of "switchDataConnection" which would switch to the testing database, but that feels dirty and hacky.


回答1:


An approach that worked for me was an env.js file and a config.js file.

In config.js (really copied from config-dev.js and config-prod.js) I put all my configuration settings. Things like directory path information and database settings.

For example, this is my dev DB connection (I'm using the knexjs package):

var knex = {
  client : 'mysql',
  connection : {
    host : '127.0.0.1',
    user : 'root',
    password : 'pass',
    database : 'testdev',
    charset : 'utf8'
  }
};

In my production config, I simply have a different connection defined.

Then, in env.js, I required the config.js file to load the appropriate settings. In my unit tests, I can load the env.js for dev, and in app.js I can load for production.

Make sense? You are on the right path though, it is possible to have the same code testing against two different DBs.

EDIT from comments: I would suggest setting up a test environment where you can start your own API server on a different port, and then run your tests off that (which of course would connect to a test DB).



来源:https://stackoverflow.com/questions/22173834/using-a-testing-database-with-restify

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!