简体   繁体   English

内置库'dart:html'在独立VM上不可用

[英]The built-in library 'dart:html' is not available on the stand-alone VM

I'd like to be able to unit test my custom polymer elements. 我希望能够对我的定制聚合物元素进行单元测试。

Given a component in lib/web_components: 给定lib / web_components中的一个组件:

class Foo extends PolymerElement {
  Foo.created() : super.created();
}

and a test in test/web_components: 和test / web_components中的测试:

main() {
  test("Should Be a PolymerElement", (){
    Foo undertest = new Foo.created();

    expect(undertest, new isInstanceOf<PolymerElement>());
  });
}

Running the test results in the error mentioned in the title. 运行测试会导致标题中提到的错误。 How can I avoid this error? 我怎样才能避免这个错误?

Edit: 编辑:

So I've tried adding @TestOn('content-shell') at the top of my client side test files, and adding @TestOn('vm') to the server side tests. 所以我尝试在客户端测试文件的顶部添加@TestOn('content-shell') ,并在服务器端测试中添加@TestOn('vm')

In Grinder I have: 在磨床我有:

@Task('Test')
test() {
  new PubApp.local('test').run([]);
  new PubApp.local('test').run(["-p", "content-shell"]);
}

The server-side tests run fine but the client-side tests throw the following error: 服务器端测试运行正常,但客户端测试会抛出以下错误:

pub run test -p content-shell test/web_component/foo_test.dart
  "Failed to start content shell: No such file or directory"

Edit 2: 编辑2:

I've tried running in the dartium platform with the following command since content-shell doesn't seem to work: 我尝试使用以下命令在dartium平台上运行,因为content-shell似乎不起作用:

pub run test:test -p dartium test/web_component/foo_test.dart

The result is: 结果是:

Failed to load "test/web_component/foo_test.dart": type 'test.backend.declarer.Declarer' is not a subtype of type 'test.backend.declarer.Declarer' of 'function result'.
  packages/test/test.dart 44:32                          _declarer
  packages/test/test.dart 108:5                          test
  foo_test.dart 9:3                     main
  package:test                                           IframeListener.start
  foo_test.dart.browser_test.dart 6:18  main

You need to create an html page for the test and run the test in the browser. 您需要为测试创建一个html页面并在浏览器中运行测试。 The new test package has a decent readme explaining how to do that. 新的test包有一个很好的自述文件,解释了如何做到这一点。 (I have an issue that browser tests time out often. This is a known issue and will probably be fixed soon.) (我有一个问题,浏览器经常测试超时。这是一个已知的问题,很快就会修复。)

Update See Instantiating polymer element via dart code , Dynamically create polymer element for how to create a Polymer element dynamically. 更新请参阅通过dart代码实例化聚合物元素动态创建聚合物元素以了解如何动态创建 Polymer元素。

Instead of main() use 而不是main()使用

@whenPolymerReady
init() {

@TestOn('content-shell') is ok but it's better to use @TestOn('browser') and then specify the concrete browser using the -p argument ( -pchrome , -pdartium , -pfirefox , -pcontent-shell , ... see test README for a list of supported browsers). @TestOn('content-shell') ,但最好使用@TestOn('browser') ,然后使用-p参数指定具体的浏览器( -pchrome-pdartium-pfirefox-pcontent-shell ,. ..请参阅测试README以获取支持的浏览器列表)。 You can pass more than one -p at a time to run the tests at more than one browser. 您可以一次传递多个-p以在多个浏览器上运行测试。

You can also use a custom HTML page for the test 您还可以使用自定义HTML页面进行测试

<!doctype html>
<!-- custom_html_test.html -->
<html>
  <head>
    <title>browser/polymer test</title>
    <link rel="import" href="app_element.html">
    <link rel="import" href="child_element.html">
    <link rel="x-dart-test" href="html_test.dart">
    <script src="packages/test/dart.js"></script>
  </head>
  <body>
    <div id="granny">
      <div>
        <div id="parent">
          <div>
            <div id="child">child</div>
          </div>
        </div>
      </div>
    </div>
    <app-element id="polymer-parent" unresolved>
      <child-element id="polymer-child"></child-element>
    </app-element>
  </body>
</html>

where the file names needs to be the same as the Dart test file except .html as file extension and <link rel="x-dart-test" href="html_test.dart"> refers to your Dart test file. 其中文件名必须与Dart测试文件相同,但.html作为文件扩展名, <link rel="x-dart-test" href="html_test.dart">指的是Dart测试文件。 app-element , child-element are Polymer elements I used in my test (like your Foo) app-elementchild-element是我在测试中使用的Polymer元素(比如你的Foo)

Another update I assume you need to use a custom HTML file (not tried without yet) for Polymer tests because otherwise there is no way to register an entry point for the transformer. 另一个更新我假设您需要使用自定义HTML文件(尚未尝试),以进行聚合物测试,否则无法为变换器注册入口点。 And then add the html file as entry point to the Polymer transformer section in pubspec.yaml 然后将html文件添加为pubspec.yaml Polymer transformer部分的入口点

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM