我开始测试我的网络组件并注意到Dart有一个很好的库,它确实可以与Dart一起使用。但现在我也想测试我的聚合物组件,但我很难做到这一点。似乎我的单元测试不能将我的元素识别为聚合物元素。 那我的"测试"看起来像现在
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:无法执行&#39; appendChild&#39; on&#39;节点&#39;:类型&#39; HTML&#39;的节点可能无法插入&#39; #document&#39;类型的节点内。
来自:polymer.concat.js:6313
由于我没有测试dart聚合物的经验,我会采取任何建议,最好是做什么。
答案 0 :(得分:2)
您需要正确的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'.
});
});
});
}
提示:不要忘记将测试的输入页面添加到聚合物变压器配置中。