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

时间:2015-06-13 21:25:48

标签: unit-testing testing dart dart-polymer

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

使用新的test库来测试Dart Polymer元素,构建my_element_test.html as prescribed。请参阅我的回购: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);
  });
}

pub run test...中的聚合物入口点添加test/my_element_test.html后,在控制台中加载没有pubspec.yaml的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. 

1 个答案:

答案 0 :(得分:5)

@whenPolymerReady上的注释main()丢失了。此外,应将测试变换器(在测试包的README.md中解释)添加到pubspec.yaml中的变换器部分。