gpt4 book ai didi

dart - 1.0的聚合物 Dart 测试

转载 作者:行者123 更新时间:2023-12-03 03:06:14 25 4
gpt4 key购买 nike

我尝试按照https://pub.dartlang.org/packages/test上的说明,使用“测试”包来测试用dart编写的聚合物组件。

pubspec.yaml中,我将转换器设置如下:

transformers:
- web_components:
entry_points:
- web/foo.html
- test/my_component_test.html
- reflectable:
entry_points:
- web/foo.dart
- test/pub_serve:
$include:
- test/**_test{.*,}.dart
my_component_test.html看起来像这样:
<!doctype html>
<html>
<head>
<title>Custom HTML Test</title>
<link rel="x-dart-test" href="my_component_test.dart">
<script src="packages/test/dart.js"></script>
</head>
<body><my-component id="abc"></my-component></body>
</html>

像这样的 my_component_test.dart:
@TestOn("browser")
import "dart:html";
import "package:test/test.dart";
import 'package:polymer/polymer.dart';

import '../web/my_component.dart';

void main() {
MyComponent componentUnderTest;

setUpAll(() {
return initPolymer();
});

setUp(() {
componentUnderTest = document.body.querySelector("#abc");
});

test("my-component should be initialized correctly", () {
Element heading = componentUnderTest.shadowRoot.querySelector('h1');
expect(heading.text, equals('This is my component'));
});
}

如果尝试在两个单独的终端中使用 pub servepub run test:test --pub-serve=8081 -p firefox运行测试,则会出现两个不同的错误:
[Error from WebComponents on polymer_dart_example|test/my_component_test.html]:
Unable to import `polymer_dart_example/web/my_component.dart` from polymer_dart_example|test/my_component_test.bootstrap.initialize.dart.
[Error from WebComponents on polymer_dart_example|test/my_component_test.html]:
Can't import `polymer_dart_example|web/my_component.dart` from `polymer_dart_example|test/my_component_test.bootstrap.initialize.dart`
Build error:
Transform WebComponents on polymer_dart_example|test/my_component_test.html threw error: Could not format because the source could not be parsed:

line 16, column 1 of <unknown>: Directives must appear before any declarations
import 'package:polymer/src/common/polymer_register.dart' as i13;

在编译和
  NullError: receiver is undefined
package:web_components/src/init.dart 31:1 J.$asx
package:polymer/init.dart 28:22 <fn>
package:stack_trace init.<fn>.<fn>
package:polymer/init.dart 31:3 dart<._setUpPropertyChanged
package:path/src/style/windows.dart 95:71 dart<.initPolymer.<fn>
===== asynchronous gap ===========================

在测试时(由 initPolymer()引起)。

我怀疑配置有问题,但是我无法弄清楚问题出在哪里。组件本身运行良好。

最佳答案

test/my_component_test.dart需要添加到reflectable转换器中。

我也很确定await initPolymer();需要在setUpAll()方法之外,至少没有理由用setUpAll()包装它。

void main() async {
MyComponent componentUnderTest;

await return initPolymer();

...
test(...);
group(...);
}

相关问题 https://github.com/dart-lang/polymer-dart/issues/649

关于dart - 1.0的聚合物 Dart 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33864930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com