1

我希望能够对我的自定义聚合物元素进行单元测试。

给定 lib/web_components 中的一个组件:

class Foo extends PolymerElement {
  Foo.created() : super.created();
}

和 test/web_components 中的测试:

main() {
  test("Should Be a PolymerElement", (){
    Foo undertest = new Foo.created();

    expect(undertest, new isInstanceOf<PolymerElement>());
  });
}

运行测试导致标题中提到的错误。我怎样才能避免这个错误?

编辑:

所以我尝试@TestOn('content-shell')在客户端测试文件的顶部添加,并添加@TestOn('vm')到服务器端测试。

在磨床我有:

@Task('Test')
test() {
  new PubApp.local('test').run([]);
  new PubApp.local('test').run(["-p", "content-shell"]);
}

服务器端测试运行良好,但客户端测试抛出以下错误:

pub run test -p content-shell test/web_component/foo_test.dart
  "Failed to start content shell: No such file or directory"

编辑2:

我尝试使用以下命令在 dartium 平台上运行,因为 content-shell 似乎不起作用:

pub run test:test -p dartium test/web_component/foo_test.dart

结果是:

Failed to load "test/web_component/foo_test.dart": type 'test.backend.declarer.Declarer' is not a subtype of type 'test.backend.declarer.Declarer' of 'function result'.
  packages/test/test.dart 44:32                          _declarer
  packages/test/test.dart 108:5                          test
  foo_test.dart 9:3                     main
  package:test                                           IframeListener.start
  foo_test.dart.browser_test.dart 6:18  main
4

1 回答 1

3

您需要为测试创建一个 html 页面并在浏览器中运行测试。新test软件包有一个不错的自述文件,解释了如何做到这一点。(我有一个浏览器测试经常超时的问题。这是一个已知问题,可能很快就会修复。)

更新 请参阅通过 dart 代码实例化聚合物元素动态创建聚合物元素以了解如何动态创建聚合物元素。

而不是main()使用

@whenPolymerReady
init() {

@TestOn('content-shell')没问题,但最好先使用@TestOn('browser'),然后使用-p参数指定具体浏览器(-pchrome, -pdartium, -pfirefox, -pcontent-shell, ... 有关支持的浏览器列表,请参阅测试自述文件)。您可以一次通过多个-p以在多个浏览器上运行测试。

您还可以使用自定义 HTML 页面进行测试

<!doctype html>
<!-- custom_html_test.html -->
<html>
  <head>
    <title>browser/polymer test</title>
    <link rel="import" href="app_element.html">
    <link rel="import" href="child_element.html">
    <link rel="x-dart-test" href="html_test.dart">
    <script src="packages/test/dart.js"></script>
  </head>
  <body>
    <div id="granny">
      <div>
        <div id="parent">
          <div>
            <div id="child">child</div>
          </div>
        </div>
      </div>
    </div>
    <app-element id="polymer-parent" unresolved>
      <child-element id="polymer-child"></child-element>
    </app-element>
  </body>
</html>

其中文件名需要与 Dart 测试文件相同,但.html文件扩展名除外,并且<link rel="x-dart-test" href="html_test.dart">引用您的 Dart 测试文件。app-elementchild-element是我在测试中使用的聚合物元素(比如你的 Foo)

另一个更新 我假设您需要使用自定义 HTML 文件(尚未尝试过)进行 Polymer 测试,否则无法为转换器注册入口点。然后将 html 文件作为入口点添加到 Polymer 转换器部分pubspec.yaml

于 2015-05-12T04:28:37.993 回答