我开始测试我的网络组件并注意到 Dart 有一个很好的库,它与 Dart 一起工作得很好。但现在我也想测试我的 Polymer 组件,但我很难做到。似乎我的单元测试没有将我的元素识别为 polymer 元素。 这就是我的“测试”目前的样子
test.dart
import "../../../components/medium/bar-chart.dart";
import "package:unittest/unittest.dart";
import 'package:polymer/polymer.dart';
import 'dart:html';
main() {
// initPolymer();
print("--- Bar Chart Test ---");
group('Bar Chart Test', () {
test(("queryForBarChart"),() {
expect(querySelector('bar-chart'), isNotNull); // PASS
});
test(("testtest"),() {
// This should pass
expect(querySelector('bar-chart').test(), equals(5));
// Test failed: Caught type 'HtmlElement' is not a subtype of type 'BarChartElement' in type cast.
// without Cast: Test failed: Caught Class 'HtmlElement' has no instance method 'test'.
});
});
}
test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>Unit Test Results</title>
<script src="../packages/web_components/platform.js"></script>
<link rel="import" href="packages/polymer/polymer.html"/>
<link rel="import" href="../../../components/medium/bar-chart.html"/>
<script src="../packages/browser/dart.js"></script>
<script type="application/dart" src="test.dart"></script>
</head>
<body
<bar-chart></bar-chart>
</body>
</html>
我也得到异常:
Uncaught HierarchyRequestError: Failed to execute 'appendChild' on 'Node': Nodes of type 'HTML' may not be inserted inside nodes of type '#document'.
来自:polymer.concat.js:6313
因为我没有任何测试 Dart polymer 的经验,所以我会听取任何最好的建议。
最佳答案
您需要适当的 Polymer 初始化代码。参见 how to implement a main function in polymer apps了解详情。
此包包含大量 Polymer.dart 单元测试,您可以将其用作示例 https://github.com/dart-lang/core-elements/tree/master/test
import "../../../components/medium/bar-chart.dart";
import "package:unittest/unittest.dart";
import 'package:polymer/polymer.dart';
import 'dart:html';
main() {
// old initPolymer().run(() {
initPolymer().then((zone) => zone.run(() {
print("--- Bar Chart Test ---");
group('Bar Chart Test', () {
test(("queryForBarChart"),() {
expect(querySelector('bar-chart'), isNotNull); // PASS
});
test(("testtest"),() {
// This should pass
expect(querySelector('bar-chart').test(), equals(5));
// Test failed: Caught type 'HtmlElement' is not a subtype of type 'BarChartElement' in type cast.
// without Cast: Test failed: Caught Class 'HtmlElement' has no instance method 'test'.
});
});
});
}
提示:不要忘记将测试的入口页面添加到 polymer 变压器配置中。
关于testing - 如何测试 polymer Dart ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27404245/