Read a data file for unit test in Dart

740 views Asked by At

I'm using this snippet to read a data file in a unit test:

var file = new File('/Users/chambery/projects/Foo/src/resources/skills.yaml');
Future<String> finishedReading = file.readAsString();
finishedReading.then((text) {
  print(text);
  print(loadYaml(text));
});

Running in the Dart Editor I get no error (but no printout),

...
PASS: calc_ranks
PASS: load_skills

All 7 tests passed.
unittest-suite-success

(edit: removed command line error; dart vm was out-of-date)

I don't need async file read.

1

There are 1 answers

0
Florian Loitsch On

I'm guessing that you don't tell the unittest framework that your test is asynchronous. The framework will therefore not wait for your asynchronous tests to finish and assume that they passed.

Use expectAsyncX (where "X" is the number of arguments) to make sure that the framework waits for your asynchronous tests to finish.

See the unittest documentation: https://api.dartlang.org/docs/channels/stable/latest/unittest.html

If you are dealing with Futures, you can also use expect(future, completes).