Loopback testing with memory as datasource
Asked Answered
F

1

10

I'm trying to use the memory-connector as a datasource when doing integration testing. But it seems to always connect to the mongodb-datasource.

One major hack i have done is to change the datasource for each model to memory. But there must be a better way to do this. I'm running my tests from a gulp-task. My roflmao model-memory-hack:

var models = require('../server/model-config.json');
  for (var key in models) {
    var model = loopback.getModel(key);
    loopback.configureModel(model, {dataSource: memory});
  }
}

Is there any way to change the datasource for the app? Or do i have to change the datasource for each individual model..?

A way of doing this is to change the environment variable during testing, but so far, no luck.. I'm doing this with the gulp-task preprocess.

Hopefully by changing the environment variable, it would use datasources.integrationtesting.js, in which i have memory as a datasource.

My gulp-task:

return gulp.src('integration-tests/*.js')
.pipe($.preprocess({context: {NODE_ENV: 'integrationtesting'}}))
.pipe($.mocha())

I'm using:

  • loopback-testing
  • gulp-mocha

Appreciate any comments.. : )

Fortunio answered 30/4, 2015 at 15:21 Comment(1)
What would fix this is to be able to change the NODE_ENV during testing...Fortunio
A
8

I think what you're looking for are environment-specific configuration files. Basically, you just create a datasource with the same name, but different implementations in different environments. Your datasources.json file would be the default, but datasources.development.json would be used if NODE_ENV was set to development.

From that linked page, you might have this in datasources.json:

{
  db: {
    connector: 'mongodb',
    database: 'myapp',
    user: 'myapp',
    password: 'secret'
  }
}

And this in datasources.development.json:

{
  db: {
    connector: 'memory'
  }
} 
Appealing answered 1/5, 2015 at 12:51 Comment(1)
As explained, i fire up the tests in a gulp task. I try to change the environment to "test" to allow for environment-specific configuration, but it doesn't seem to use the test-configs..Fortunio

© 2022 - 2024 — McMap. All rights reserved.