如何使用新的测试库测试 Dart Polymer 元素?

How to test Dart Polymer elements using the new Test library?

你如何测试 Polymer elements using the new test 库?

使用新的test library to test a Dart Polymer element, I build my_element_test.html as prescribed. Please see my repo: polymer-dart-testing

没有聚合物引发通过

my_element_test.htmlmy_element_test.dart(注释掉聚合物引发)按预期通过测试:

my_element_test.html

<!doctype html>
<html>
  <head>
    <title>My Element Test</title>
    <link rel="import" href="packages/polymer_dart_testing/my_element.html">
    <link rel="x-dart-test" href="my_element_test.dart">
    <script src="packages/test/dart.js"></script>
  </head>
  <body>
    <div>Custom HTML Test is Custom.</div>
    <my-element></my-element>
  </body>
</html>

my_element_test.dart

import 'package:test/test.dart';
import 'package:polymer_dart_testing/my_element.dart';
import 'package:polymer/polymer.dart';

import 'dart:html';

main() {

  setUp(() async {
    // await initPolymer();
    // return await Polymer.onReady;
  });

  test('custom_html_test', (){
    expect(true, isTrue);
  });
}

pubspec.yaml.

中的聚合物入口点添加 test/my_element_test.html 后,在没有 pub run test... 的情况下加载 Dartium 会进入控制台并显示自定义元素

pubspec.yaml

transformers:
- polymer:
    entry_points:
     - web/index.html
     - test/my_element_test.html

my_element_test.html

<!doctype html>
<html>
  <head>
    <title>My Element Test</title>
    <link rel="import" href="packages/polymer_dart_testing/my_element.html">
  </head>
  <body>
    <div>Custom HTML Test is Custom.</div>
    <my-element></my-element>
    <script type="application/dart" src="my_element_test.dart"></script>
  </body>
</html>

my_element_test.dart

import 'package:test/test.dart';
import 'package:polymer_dart_testing/my_element.dart';
import 'package:polymer/polymer.dart';

import 'dart:html';

main() {

  setUp(() async {
    await initPolymer();
    return await Polymer.onReady;
  });

  test('custom_html_test', (){
    expect(true, isTrue);
  });
}

但是,pub run test... 在启动 Polymer 并添加到 pubspec 入口点时失败。

$ pub serve
Loading source assets... 
Loading polymer and test/pub_serve transformers... 
Serving polymer_dart_testing web  on http://localhost:8080
Serving polymer_dart_testing test on http://localhost:8081
Build completed successfully

...
...
/my_element_test.html.polymer.bootstrap.dart.browser_test.dart → 
Could not find asset polymer_dart_testing|test/my_element_test.html.polymer.bootstrap.dart.browser_test.dart.
$ pub run test --pub-serve=8081 -p content-shell
"pub serve" is compiling test/my_element_test.dart...
00:00 +0: load error                                                            00:00 +0 -1: load error                                                                             
  Failed to load "test/my_element_test.dart": Failed to load script at http://localhost:8081/my_element_test.html.polymer.bootstrap.dart.browser_test.dart.
00:00 +0 -1: Some tests failed. 

缺少 main() 上的注释 @whenPolymerReady。此外,应将测试变压器(在测试包的 README.md 中进行说明)添加到 pubspec.yaml 中的变压器部分。