Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

RSpec + jasmine-node

RSpec + jasmine-node

Problem

I was wondering if anyone has already solved a problem I am experiencing.

I am currently working on a project with processes split between Rails and a Nodejs process. I use RSpec for Rails based testing and jasmine-node for the Node counterpart.

My situation is that both the Rails and Node apps access the same Database. My problem is that RSpec makes it trivial in setting a TEST database, seeding it with some values and running tests against these but jasmine-node has no such mechanism.

Instead, my jasmine-node database related tests have no mechanism to setup a TEST database so my jasmine-node tests instead rely on my DEVELOPMENT database. To me this is both wrong and bad as these tests are not repeatable.

Currently my jasmine-node tests are in a separate directory and outside of Rails' RSpec spec folder (and run via the jasmine-node command line). Recently I learnt that RSpec can drive jasmine client javascript tests. Does a similar mechanism exist to drive jasmine-node tests from RPsec?

Has anyone else encountered this problem? If so, I am very eager to hear how you've approached and tackled this problem.

My thanks and gratitude in advance.

Problem courtesy of: Nazar

Solution

My solution was to use an environment variable TEST to specify whether to use the test or development database. This example, which uses Sequelize, works with test and dev databases on both the local machine and Heroku.

var config = {}

if (typeof (process.env.DATABASE_URL) != 'undefined') {
    var url = require('url');

    var dbUrl;

    if (process.env.TEST == 'true') {
        dbUrl = url.parse(process.env.HEROKU_POSTGRESQL_NAVY_URL);
    }
    else {
        dbUrl = url.parse(process.env.DATABASE_URL);
    }

    var authArr = dbUrl.auth.split(':');

    config.database = dbUrl.path.substring(1);
    config.username = authArr[0];
    config.password = authArr[1];
    config.host = dbUrl.hostname;
    config.port = dbUrl.port;
    config.dialect = 'postgres';
    config.protocol = 'postgres';
    config.importBatchSize = 1000;
}
else {
    if (process.env.TEST == 'true') {
        console.log('DB:  test');

        config.database = 'test';
        config.username = 'postgres';
        config.password = 'postgres';
        config.host = '127.0.0.1';
        config.port = 5432;
        config.dialect = 'postgres';
        config.protocol = 'tcp';
        config.importBatchSize = 1000;
    }
    else {
        console.log('DB:  db');

        config.database = 'db';
        config.username = 'postgres';
        config.password = 'postgres';
        config.host = '127.0.0.1';
        config.port = 5432;
        config.dialect = 'postgres';
        config.protocol = 'tcp';
        config.importBatchSize = 1000;
    }
}

config.logging = false;
//config.logging = console.log;

module.exports = config;

Then, each jasmine-node test that accesses the database does a require on a file containing the following bootstrapping code:

process.env['TEST'] = 'true'; // Use test database
var config             = require('../config/config.js');
process.env['TEST'] = 'false'; // Stop using test database after this

var sequelizeSingleton = require("../classes/model.js");
sequelizeSingleton.setup(path.normalize(__dirname + '/../models'), config.database, config.username, config.password,
    { dialect: config.dialect, protocol: config.protocol, host: config.host, port: config.port, omitNull: true, logging: config.logging });
Solution courtesy of: dankohn

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

RSpec + jasmine-node

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×