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.
I just re-read your question, and I realize I was answering sort of the wrong question...
I believe you're using
expectAsync
incorrectly.expectAsync
is used to wrap a callback with N parameters and ensure that it rancount
times (default 1).expectAsync
will ensure that any exceptions are caught by the test itself and are returned. It does not actually run any expectations by itself (bad nomenclature.)What you want is just:
or if you need to ensure that the test completed to some particular value:
Another way of doing this is to use the
completes
matcher.or to test for an exception:
If you want to check the completed value, you could do as follows:
See:
A
Future
can be returned from atest()
method - this will cause Unit Test to wait for theFuture
to complete.I usually put my
expect()
calls in athen()
callback. For example: