gpt4 book ai didi

database - Aqueduct :状态不佳:未找到 '_MyEntity' 的实体。您是否忘记创建一个 'ManagedContext' ?

转载 作者:行者123 更新时间:2023-12-03 03:37:39 24 4
gpt4 key购买 nike

我正在开始一个新项目并希望使用测试驱动开发。这是我的实体:

import 'package:aqueduct/aqueduct.dart';

class MyEntity extends ManagedObject<_MyEntity> implements _MyEntity {}class _MyEntity {
@primaryKey
int id;
int myValue;
}

当我从文本文件中提取一些数据时,我想独立于数据库使用 MyEntity。但是当我尝试像这样测试它时
void main() {
test('DatabaseBuilder returns multiple entities', () {
List<MyEntity> entities = [];
entities.add(MyEntity());
expect(entities.length, greaterThan(0));
});
}

我收到以下错误:

Bad state: No entity found for '_MyEntity. Did you forget to create a 'ManagedContext'?



我不允许将实体用于非数据库逻辑吗?

最佳答案

This question已在 Aqueduct Slack channel 上得到答复所以我把它移到这里更容易搜索。

Reductions answered :

You will need to start a TestHarness with TestHarnessORMMixin mixed in. After that you can used the MnagedObject (sic) however you find fit.



joeconwaystk followed up :

Yes, what Reductions said… the framework handles TDD with the ORM by creating a temporary schema in your database server during testing (with the TestHarnessORMixin)



所以我更新了 test/harness/app.dart 文件,看起来像这样:
class Harness extends TestHarness<MyChannel> with TestHarnessORMMixin {
@override
Future onSetUp() async {
await resetData();
}

@override
Future onTearDown() async {}

@override
ManagedContext get context => channel.context;
}

我的测试看起来像这样:
Future main() async {
final harness = Harness()..install();

test('DatabaseBuilder returns multiple entities', () {
List<MyEntity> entities = [];
entities.add(MyEntity());
expect(entities.length, greaterThan(0));
});
}

即使我没有直接使用线束,安装它也足以消除错误。

如果您不喜欢该方法,我发现的另一个选择是创建一个模型类,该类镜像 _MyEntity 而不扩展 ManagedObject:
class MyEntityModel implements _MyEntity {
@override
int id;

@override
int myValue;
}

然后可以在实际插入数据库时​​将其映射到 MyEntity。安装测试工具并直接使用 MyEntity 似乎更好,这就是我所做的。

有关设置测试的更多帮助,请参阅 this videodocumentation .

关于database - Aqueduct :状态不佳:未找到 '_MyEntity' 的实体。您是否忘记创建一个 'ManagedContext' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59779847/

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