渡槽:错误状态:找不到“_MyEntity”的实体。您是否忘记创建 'ManagedContext'?
Aqueduct: Bad state: No entity found for '_MyEntity'. Did you forget to create a 'ManagedContext'?
我正在开始一个新项目并希望使用测试驱动 development.This 是我的实体:
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 was answered on the Aqueduct Slack channel 所以我把它移到这里是为了更容易搜索。
You will need to start a TestHarness
with TestHarnessORMMixin
mixed
in. After that you can used the MnagedObject (sic) however you find fit.
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));
});
}
即使我没有直接使用线束,安装它也足以消除错误。
如果您不喜欢这种方法,我发现的另一个选择是创建一个模型 class 来镜像 _MyEntity 而不扩展 ManagedObject:
class MyEntityModel implements _MyEntity {
@override
int id;
@override
int myValue;
}
这可以在实际插入数据库时映射到 MyEntity。只安装测试工具并直接使用 MyEntity 似乎更好,所以我就是这样做的。
有关设置测试的更多帮助,请参阅 this video and the documentation。
对我来说,实例化一个 ManagedContext
:
就足够了
void main() {
final dataModel = ManagedDataModel.fromCurrentMirrorSystem();
final _ = ManagedContext(dataModel, null);
// my tests go here
}
我做一个简单的单元测试,不需要数据库。
我正在开始一个新项目并希望使用测试驱动 development.This 是我的实体:
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 was answered on the Aqueduct Slack channel 所以我把它移到这里是为了更容易搜索。
You will need to start a
TestHarness
withTestHarnessORMMixin
mixed in. After that you can used the MnagedObject (sic) however you find fit.
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));
});
}
即使我没有直接使用线束,安装它也足以消除错误。
如果您不喜欢这种方法,我发现的另一个选择是创建一个模型 class 来镜像 _MyEntity 而不扩展 ManagedObject:
class MyEntityModel implements _MyEntity {
@override
int id;
@override
int myValue;
}
这可以在实际插入数据库时映射到 MyEntity。只安装测试工具并直接使用 MyEntity 似乎更好,所以我就是这样做的。
有关设置测试的更多帮助,请参阅 this video and the documentation。
对我来说,实例化一个 ManagedContext
:
void main() {
final dataModel = ManagedDataModel.fromCurrentMirrorSystem();
final _ = ManagedContext(dataModel, null);
// my tests go here
}
我做一个简单的单元测试,不需要数据库。