Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dart unit testing classes in a html-dependant library

I have a library which uses CanvasElement and therefor is dependent on dart:html. Now I'm trying to write unit-tests for that same library. However, I get the following error when trying to run them:

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

Here is how my test file looks like:

library PiflexUmlTest;

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

part 'src/geometry/vector_test.dart';

main () {
  testVector();
}

I understand it's failing because library itself in lib.dart file has a line stating:

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

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

Even though library itself is dependent on it, I'm not trying to test a class which has anything to do with HTML.

What are my solutions here? Is there a way to just import classes I want to test without importing the whole lib? Or do I have to split my lib into html-dependent part and non-html-dependent part?

like image 204
Igor Pantović Avatar asked Mar 03 '14 17:03

Igor Pantović


2 Answers

You could run browser based unit tests with content_shell (headless browser).

The folder where you installed DartEditor to (darteditor/chromium/download_contentshell.sh) contains a script file to download the part containing content_shell.

You need an HTML file that is run by content_shell and that runs the tests. The HTML file could look like

<!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>

Dart unit tests

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

main() {
  useHtmlConfiguration();

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

Maybe overkill for your use case, but still a solution.

EDIT
There is also a discussion going on about this problem: https://groups.google.com/a/dartlang.org/forum/#!topic/misc/pacB66gnVcg

like image 141
Günter Zöchbauer Avatar answered Nov 20 '22 17:11

Günter Zöchbauer


This appears to be fixed in newer versions of the test package. There is a new parameter '--platform' that accepts a browser as a value so:

pub run test --platform chrome

Will compile your tests to javascript and run them on chrome.

You can also use the @TestOn annotation, or the testOn parameter to test() or group() to specify 'vm' or 'browser' if you're writing a library that should work in both locations.

See https://pub.dartlang.org/packages/test#browservm-hybrid-tests

like image 1
Jeff Avatar answered Nov 20 '22 17:11

Jeff