6
6

More than 5 years have passed since last update.

dart:htmlに依存したコードのユニットテスト

Last updated at Posted at 2014-07-14

問題

dart:htmlに依存したライブラリコードのテストは普通にやると

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

というふうに怒られる。

解決方法

1. テストコード側で準備

package:unittest/html_config.dartをimportして、mainメソッドの最初にuseHtmlConfiguration();する。

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

void main() {
  useHtmlConfiguration();
  test("html test", () {
   ...
  });
}

2. テスト結果表示用のHTMLファイルを配置する

テスト表示用のHTMLファイルを用意(html_test.htmlとする)

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>html_test</title>
</head>
<body>

<script type="application/dart" src="html_test.dart"></script>
<script src="packages/browser/dart.js"></script>
</body>
</html>

ポイントはhtml_test.dartをHTML内で呼び出すことです。

3. $ pub serveする

ちゃんとtestディレクトリにテストコードを書いていれば、

Serving my_app web  on http://localhost:9000
Serving my_app test on http://localhost:9001

のようにログが流れるはず。

4. テスト用ページにアクセスする

先ほど配置したhttp://localhost:9001/html_test.htmlをブラウザで開く。

これでテスト結果がHTML形式で出力される。

6
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
6