微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Dart Aqueduct Rest API 单元测试:空安全错误

如何解决Dart Aqueduct Rest API 单元测试:空安全错误

我使用 aqueduct 和 dart 创建了一个简单的 REST Api,我正在尝试运行单元测试。但是,尝试这样做时出现以下错误

Failed to load "test\example_test.dart":
  Unable to spawn isolate: Error: Cannot run with sound null safety,because the following dependencies
  don't support null safety:

   - package:test_core
   - package:test_api
   - package:aqueduct_test
   - package:test
   - package:aqueduct
   - package:boolean_selector
   - package:logging
   - package:safe_config
   - package:matcher
   - package:crypto
   - package:password_hash
   - package:open_api
   - package:yaml
   - package:postgres
   - package:convert
   - package:codable
   - package:pub_semver

我的代码如下:

channel.dart

Controller get entryPoint {
    final router = Router();

    // Prefer to use `link` instead of `linkFunction`.
    // See: https://aqueduct.io/docs/http/request_controller/
    router.route("/example").linkFunction((request) async {
      return Response.ok({"key": "value"});
    });

example_test.dart

import 'harness/app.dart';

Future main() async {
  final harness = Harness()..install();

  test("GET /example returns 200 {'key': 'value'}",() async {
    expectResponse(await harness.agent.get("/example"),200,body: {"key": "value"});
  });
}

感谢任何帮助

解决方法

好像你的 Dart 版本是 >2.12

Aqueduct 不支持空安全。所以基本上你不能照原样去做。您需要安装低于 2.12 的 dart 版本。我建议使用 2.7.2,因为 2.8 及更高版本有一些重大更改,这些更改在 Aqueduct 中造成了混乱。

更好的解决方案是在 docker 容器中安装 dart 2.7.2 并使用 VS 代码进行开发。

另外最好注意的是,Stabelkernel discontinued 开发了 Aqueduct,它不会得到支持。

社区正在努力将渡槽分叉并将其重新命名为 Conduit。希望 Conduit 的空安全初始版本即将发布。所以也要注意这一点。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。