2015-05-12 43 views
1

我希望能夠對我的定製聚合物元素進行單元測試。獨立VM上沒有內置庫「dart:html」

給定一個部件在LIB/web_components:

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

和在測試/ web_components測試:

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

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

運行在在標題中提到的錯誤的測試結果。我怎樣才能避免這個錯誤?

編輯:

所以我試着在我的客戶端測試文件的頂部添加@TestOn('content-shell'),並添加@TestOn('vm')到服務器端的測試。

在磨牀我:

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

服務器端的測試運行正常,但在客戶端測試拋出以下錯誤:

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

編輯2:

我我嘗試在dartium平臺上運行以下命令,因爲content-shell似乎不起作用:

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

結果是:

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 

回答

3

您需要創建一個測試用的HTML頁面,並運行在瀏覽器中測試。新的test包有一個體面的自述文件解釋如何做到這一點。 (我有一個問題,瀏覽器測試超時頻繁。這是一個已知問題,可能會盡快修復。)

更新Instantiating polymer element via dart codeDynamically create polymer element如何動態地創建一個聚合物元件。

代替main()使用

@whenPolymerReady 
init() { 

@TestOn('content-shell')是確定的,但最好使用@TestOn('browser'),然後指定使用-p參數(-pchrome-pdartium-pfirefox-pcontent-shell具體的瀏覽器,...查看測試自述文件支持的瀏覽器列表)。您可以一次傳遞多個-p以在多個瀏覽器上運行測試。

您也可以使用自定義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> 

該文件的名稱必須相同,除了.html的文件擴展名和<link rel="x-dart-test" href="html_test.dart">達特測試文件是指你的鏢測試文件。app-elementchild-element是我在我的測試,所用聚合物的元素(如您的Foo)

另一個更新 我假設你需要使用的聚合物測試自定義的HTML文件(不包括尚未嘗試過),否則有沒有辦法爲變壓器註冊入口點。 然後將html文件作爲入口點添加到pubspec.yaml

+0

再次更新,因爲我忘記了您需要註冊變壓器的入口點。 –

+0

感謝您的更新。目標是讓這些測試在持續集成服務器上運行。這就是爲什麼我嘗試使用'content-shell'的原因,因爲我認爲我需要一個無頭虛擬機來管理drone.io。 – ZeroDivide

+1

您可以在測試文件中使用通用的'@TestOn('browser')',然後在運行測試'pub run test -pcontent-shell'時指定具體的瀏覽器。 –