简体   繁体   English

替换Loopback测试环境中的数据源

[英]Replace datasource in test environment for Loopback

The default acceptance test for PingController created on project startup fails because my application has a Postgresql datasource that is not reachable in test environment.在项目启动时创建的 PingController 默认验收测试失败,因为我的应用程序具有在测试环境中无法访问的 Postgresql 数据源。 I try to replace this datasource by one in memory but it doesn't work, it still uses the "real" one.我尝试用 memory 中的一个替换这个数据源,但它不起作用,它仍然使用“真实”的那个。

I changed the setupApplication method this way:我以这种方式更改了setupApplication方法:

export async function setupApplication(): Promise<AppWithClient> {
  const restConfig = givenHttpServerConfig({
  });

  const app = new MyApplication({
    rest: restConfig,
  });

  const datasource = new juggler.DataSource({
    name: 'myds',
    connector: 'memory',
  });
  app.bind('datasources.myds').to(datasource);

  await app.boot();
  await app.start();

  const client = createRestAppClient(app);

  return {app, client};
}

What am I doing wrong?我究竟做错了什么?

Tanks for your help.坦克为您提供帮助。

app.boot() scans the project root for artifacts and will override the bindings. app.boot()扫描项目根目录中的工件并将覆盖绑定。

For unit tests or tests that are limited to only a few components, it's preferred to remove app.boot() and then explicitly bind each artifact that's required for that test.对于单元测试或仅限于少数组件的测试,最好删除app.boot() ,然后显式绑定该测试所需的每个工件。 This will make it easier to detect unexpected artifact dependencies.这将使检测意外的工件依赖关系变得更加容易。

Otherwise, ensure that app.boot() is called before any manual bindings:否则,请确保在任何手动绑定之前调用app.boot()

export async function setupApplication(): Promise<AppWithClient> {
  const restConfig = givenHttpServerConfig({
  });

  const app = new MyApplication({
    rest: restConfig,
  });

  const datasource = new juggler.DataSource({
    name: 'myds',
    connector: 'memory',
  });

  await app.boot();
  
  // Move manual bindings after `app.boot()`
  app.bind('datasources.myds').to(datasource);

  await app.start();

  const client = createRestAppClient(app);

  return {app, client};
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM