运行 在 Aqueduct 中对真实数据库进行 Dart 测试

Running Dart tests on the real database in Aqueduct

我正在创建一个只读的 PostgreSQL 数据库。由于填充后不会对其进行修改,因此我想 运行 进行一些测试以确保数据良好。带有 ORM mixin 的 TestHarness 为每个测试创建一个空数据库。我可以从测试中访问真实数据库吗?

我正在从 Aqueduct Slack 频道移出一个问答以供 public 参考。

来自Reductions的回答:

You can start a server and get its mangedContext.:

final app = Application<YourChannel>() 
  ..options.configurationFilePath = theConfigString 
  ..options.port = somePort;
await app.startOnCurrentIsolate();
final context = app.channel.context; 
// Now you can use the context to access the data base

这就是我在上下文中实现它的方式:

import "package:test/test.dart";
import 'package:my_server/model/text_line.dart';
import '../harness/app.dart';

Future main() async {

  // setup
  final app = Application<MyServerChannel>()
    ..options.configurationFilePath = 'config.yaml';
  await app.startOnCurrentIsolate();
  final context = app.channel.context;

  test('Database has the right number of rows', () async {
    final query = Query<TextLine>(context);
    final numRows = await query.reduce.count();
    expect(numRows, 43845);
  });

}