gpt4 book ai didi

node.js - 使用测试数据库测试 Strongloop REST api

转载 作者:搜寻专家 更新时间:2023-10-31 23:47:21 24 4
gpt4 key购买 nike

我正在使用 Strongloop 开发一个将在 Bluemix(云平台服务)上运行的 Web 应用程序。

我的问题是,当我进行测试时,我希望测试针对另一个数据库而不是内存数据库运行。

关于如何做到这一点,我有两个问题:

  1. 我是否/如何配置运行测试时应该使用的特定数据库?作为部署的一部分,当我在 Bluemix 上部署时,我希望能够运行测试。因此,如果我没记错的话,如果我可以手动设置一些参数是不够的,当我执行“Node ”时,数据库将运行什么。?

  2. 同样在我的 server.js 中,我这样做是为了将我的数据库与我的数据模型同步:

    var appModels = ['User'];
    var ds = app.dataSources.eventSeedElephantSQLDb;
    ds.isActual(appModels, function(err, actual) {
    if (!actual) {
    ds.autoupdate(appModels, function(err) {
    if (err) throw (err);
    });
    }
    });

当我运行测试时,我想运行类似的东西,但我想迁移。

在测试中我使用了 mocha、chai 和 chaiHttp。

最佳答案

您可以为测试创建“特定于环境的配置”。看:https://docs.strongloop.com/display/public/LB/Environment-specific+configuration

例如,您创建另一个 datasources.json 配置文件,但名称为 datasources.test.json

{
"my-test-database": {
"host": "localhost",
"port": 27017,
"database": "my-test-database",
"connector": "mongodb"
}
}

在测试的第一行,你定义了环境

process.env.NODE_ENV = 'test';

//here I clean and create the data that I need, but you can use your database data
beforeEach(function(done) {
app.models['City'].destroyAll();
app.models['City'].create({name: 'city test', country: 'Brazil'});
});

describe('/city', function() {
it('should find a city', function(done) {
request(app).get('/api/city').expect(200);
});
});

关于node.js - 使用测试数据库测试 Strongloop REST api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34160592/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com