如何为web项目设置unittest

时间:2015-01-31 17:44:11

标签: unit-testing dart dart-editor

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

我用Dart写了一个web应用程序,它位于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'; - 行。

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

更新(纳入GünterZöchbauer的评论)

好的,我创建了index.html来调用test.dart,但是现在我似乎对测试中的lib的相对导入有问题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

1 个答案:

答案 0 :(得分:0)

您需要使用测试脚本创建一个网页index.html作为脚本标记,并像任何Dart Web应用一样启动它。您也可以使用content_shell以非交互方式运行它。 Dart SDK包含一个批处理脚本,可以下载并安装content_shell(它不会在SDK下载中保留合理的下载大小。

http://pub.dartlang.org还提供了一个包test_runner,可帮助您运行test目录中的所有测试。