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

标签 node.js mocha.js ibm-cloud loopbackjs strongloop

我正在使用 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/

相关文章:

node.js - 如何在expressjs中使用streamlinejs?

node.js - 如何访问和测试 node.js 模块中的内部(非导出)函数?

reactjs - 类型 'toBeInTheDocument' 上不存在属性 'Matchers<any>'

node.js - IBM 沃森助手 : How to enable user metrics

json - 如何更改 package.json 中的名称属性?

javascript - 为什么嵌入式下拉菜单只选择第一个选项?

javascript - 使用 Node.js 删除 txt 文件中的一行

javascript - Protractor - 查找所有元素和找到的元素的循环长度,然后单击按钮

caching - redis和datacache的区别?

java - Spring MVC 和 BlueMix