After reading the Unit Testing with Dart somehow I'm still can not understand how to use it with Future
s.
For example:
void main() {
group('database group', () {
setUp(() {
// Setup
});
tearDown(() {
// TearDown
});
test('open connection to local database', () {
DatabaseBase database = null;
expect(database = new MongoDatabase("127.0.0.8", "simplechat-db"),
isNotNull);
database.AddMessage(null).then((e) {
expectAsync1(e) {
// All ok
}
}, onError: (err) {
expectAsync1(bb) {
fail('error !');
}
});
});
// Add more tests here
});
}
So in the test I create an instance of base abstract class DatabaseBase
with some parameters to actual MongoDb class and immediately check if it created. Then I just run some very simple function: AddMessage
. This function defined as:
Future AddMessage(String message);
and return completer.future
.
If passed message
is null then the function will fail completer as: .completeError('Message can not be null');
In actual test I want to test if Future
completed successfully or with error. So above this is my try to understand how to test Future
returns - the problems is this this test does not fail :(
Could you write in answer a little code example how to test functions that return Future
? And in test I mean - sometimes I want to test return (on success) value and fail test if success value is incorrect and another test should fail then function will fail Future
and enter to onError:
block.