I’m trying to convert the following Node.js test to Dart:
var fs = require('fs');
exports.asyncTest = function(test){
fs.stat('test.txt', function(err, stats) {
test.expect(2);
test.strictEqual(err, null);
test.notStrictEqual(stats.size, 0);
test.done();
})
};
So far I have:
import 'package:unittest/unittest.dart';
import 'dart:io';
main() {
test('File is not empty', () {
var stats = new File('test.txt').length().then(
expectAsync1((v) {
expect(v, isNot(0));
}));
});
}
This works but I’d like the test to fail instead of terminating if the file is not found. How is this accomplished?
You can use the exists() method on File.
File.exists() returns a Future<bool>, so do something like this.
And of course this would be a lot prettier if it was made into two tests. A “file exists”, and a “file not empty”, in which the latter is only run if the first one passes.
But I have not been looking enough into the unittest library to see how you can make tests dependant of each other.