1

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

1 Answer 1

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).

Sign up to request clarification or add additional context in comments.

4 Comments

Edit: apologies for the ackward formatting of this answer. Can't seem to markdown reply to a comment without answering my own question. Definately makes sense. I actually have a file conf.js with module.exports = _.extend( require(__dirname + '/../config/env/all.js'), require(__dirname + '/../config/env/' + process.env.NODE_ENV + '.json') || {} ); Problem is it can't switch connections on the fly. The testclient needs an already running instance of the API server, and switching to a new database from the test can't be done.
Oh, I see what you mean now. I am not familiar with switching DB connections on the fly while an application is running, though that does not sounds like a good idea to me. Somehow changing your in-production application DB connection to run some tests just sounds like a really risky setup. 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).
Your approach with running a separate instance for the tests makes a lot of sense. I'll switch from their JSONClient to a separate client that runs its own instance, which makes the env vars work correclty. I'll thus accept your answer above! Cheers :)
Sounds good! You can still use the restify Client in your tests to talk to your API, but I think you'll find having control over the server and DB during test cases to be helpful.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.