[英]Loopback testing with memory as datasource
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.
但似乎总是连接到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.
我正在从gulp任务中运行我的测试。 My roflmao model-memory-hack:
我的roflmao模型 - 记忆 - 黑客:
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. 这样做的一种方法是在测试期间更改环境变量,但到目前为止,没有运气..我正在使用gulp-task预处理执行此操作。
Hopefully by changing the environment variable, it would use datasources.integrationtesting.js, in which i have memory as a datasource. 希望通过更改环境变量,它将使用datasources.integrationtesting.js,其中我将内存作为数据源。
My gulp-task: 我的任务:
return gulp.src('integration-tests/*.js')
.pipe($.preprocess({context: {NODE_ENV: 'integrationtesting'}}))
.pipe($.mocha())
I'm using: 我正在使用:
Appreciate any comments.. : ) 感谢任何评论.. :)
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
. 您的
datasources.json
文件将是默认文件,但如果将NODE_ENV
设置为development
,则将使用datasources.development.json
。
From that linked page, you might have this in datasources.json: 从该链接页面,您可能在datasources.json中有此:
{
db: {
connector: 'mongodb',
database: 'myapp',
user: 'myapp',
password: 'secret'
}
}
And this in datasources.development.json: 这在datasources.development.json中:
{
db: {
connector: 'memory'
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.