gpt4 book ai didi

unit-testing - 如何为 web 项目设置单元测试

转载 作者:行者123 更新时间:2023-12-03 04:06:03 27 4
gpt4 key购买 nike

我在 DartEditor(1.8.5) 中正确配置我的第一个单元测试时遇到问题。

我用位于 web 文件夹中的 Dart 编写了一个 web 应用程序。
现在我添加了一个与 web 平行的测试文件夹,其中包含一个名为 test.dart 的文件。 :

library dartingflame_test;

import 'dart:math';
import 'package:unittest/unittest.dart';
import 'package:unittest/html_config.dart';
import 'package:mockito/mockito.dart';
import '../web/dartingflame.dart';

void main() {
useHtmlConfiguration();

test("verify robot's direction after move", verifyRobotDirectionAfterMove);
}

void verifyRobotDirectionAfterMove() {...}

不幸的是,我现在无法运行这个测试文件。
  • 使用 Dartium 运行 导致消息:Unable to locate associated html file
  • 运行导致:Observatory listening on http://127.0.0.1:49919
    The built-in library 'dart:html' is not available on the stand-alone VM.
    'package:unittest/html_config.dart': error: line 10 pos 1: library handler failed
    import 'dart:html';

  • 使用 useVMConfiguration()而不是 useHtmlConfiguration()没有帮助。测试执行仍然提示 import 'dart:html'; -line 在被测库中。

    我在 "Unit Testing with Dart" 中找不到解决方案-文章。

    更新(包含 Günter Zöchbauer 的评论)

    好的,我创建了调用 test.dart 的 index.html,但现在我似乎对被测库的相对导入有问题 import '../web/dartingflame.dart'; :

    Failed to load resource: the server responded with a status of 404 (Not Found)
    http://localhost:8081/web/dartingflame.dart
    An error occurred loading file: http://localhost:8081

    最佳答案

    您需要创建一个网页index.html将测试脚本作为脚本标签,像任何 Dart 网络应用程序一样启动它。您也可以使用 content_shell 以非交互方式运行它。 . Dart SDK 包含一个批处理脚本,用于下载和安装 content_shell (它被排除在 SDK 下载之外,以保持合理的下载大小。

    还有一个包test_runner可在 http://pub.dartlang.org这有助于在 test 中运行所有测试目录。

    关于unit-testing - 如何为 web 项目设置单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28254608/

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