2014-03-03 72 views
7

我有一個庫,它使用CanvasElement,因此依賴於dart:html。現在我正在爲同一個庫編寫單元測試。不過,我試圖運行它們時會出現以下錯誤:html依賴庫中的Dart單元測試類

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

這裏是我的測試文件看起來像:

library PiflexUmlTest; 

import 'package:PiflexUml/lib.dart'; 
import 'package:unittest/unittest.dart'; 

part 'src/geometry/vector_test.dart'; 

main() { 
    testVector(); 
} 

我理解它的失敗,因爲庫本身的lib.dart文件中有一個在線聲明:

library PiflexUml; 
// .... 
import 'dart:html'; 

part "blahblah.dart"; 
part "something_else.dart" 
// .... 

即使庫本身是依賴於它,我並不想測試它有一個任何物品類用HTML做。

我在這裏的解決方案是什麼?有沒有辦法只導入我想測試的類而不導入整個庫?或者我必須將我的lib分解爲依賴於html的部分和非html依賴的部分?

回答

5

你可以用content_shell(無頭瀏覽器)運行基於瀏覽器的單元測試。

將DartEditor安裝到(darteditor/chromium/download_contentshell.sh)的文件夾包含一個腳本文件,用於下載包含content_shell的零件。

您需要一個由content_shell運行並運行測試的HTML文件。 HTML文件可能看起來像

<!doctype html> 
<html> 
    <body> 
    <script src="packages/unittest/test_controller.js"></script> 
    <script type="application/dart" src="browser_tests.dart"></script> <!-- your unit tests --> 
    <script src="packages/browser/dart.js"></script>  </body> 
</html> 

飛鏢單元測試

import 'package:unittest/unittest.dart'; 
import 'package:unittest/html_config.dart'; 

main() { 
    useHtmlConfiguration(); 

    test('test scope',() { 
    ... 
    }); 
} 

也許矯枉過正你的使用情況,但仍然是一個解決方案。

編輯
還有一個討論會關於這個問題:‘文件://’我約CORS resctricion「進口資源從原產 https://groups.google.com/a/dartlang.org/forum/#!topic/misc/pacB66gnVcg

+0

content_shell和polymer.dart拋出異常已被跨源資源共享策略阻止加載「 – estin

+1

抱歉,我忘了 - 允許從文件訪問文件 – estin