Async unit test pattern

96 views
Skip to first unread message

Günter Zöchbauer

unread,
Apr 23, 2014, 2:23:34 AM4/23/14
to mi...@dartlang.org
In async unit test I often see expectAsync used like this

Timer.run(expectAsync((){
  expect
(true, equals(true));
}));


I think this is wrong because the test could already have been ended before `expectAsync()` is called.
I would use it like

var done = expectAsync((){});
Timer.run((){
  expect
(true, equals(true));
 
done();
}));

Can the first version work?

I also read several times that returning a Future from a test would make the test wait until the Future completes.
This didn't work for me yet (worked for async calls in `setUp()` though)

Any thoughts about this?

Florian Loitsch

unread,
Apr 23, 2014, 3:21:18 AM4/23/14
to General Dart Discussion
On Wed, Apr 23, 2014 at 8:23 AM, Günter Zöchbauer <gzo...@gmail.com> wrote:
In async unit test I often see expectAsync used like this


Timer.run(expectAsync((){
  expect
(true, equals(true));
}));


I think this is wrong because the test could already have been ended before `expectAsync()` is called.
I would use it like

var done = expectAsync((){});
Timer.run((){
  expect
(true, equals(true));
 
done();
}));

Can the first version work?
yes. You can rewrite your first example (with same semantics) as:
var t = expectAsync(() {
  expect(true, equals(true));
});
Timer.run(t);

So the expectAsync is called synchronously and not just when Timer.run is executed.

I also read several times that returning a Future from a test would make the test wait until the Future completes.
This didn't work for me yet (worked for async calls in `setUp()` though)

Any thoughts about this?

--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new

To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.



--
Give a man a fire and he's warm for the whole day,
but set fire to him and he's warm for the rest of his life. - Terry Pratchett

Peter Ahé

unread,
Apr 23, 2014, 3:31:24 AM4/23/14
to mi...@dartlang.org
Perhaps I'm missing something, but I don't think that Timer.run can affect the evaluation order of its arguments. 

In this code:

Timer.run(expectAsync(...));

You should be able to rely on expectAsync being called synchronously. 

Cheers,
Peter

Peter Ahé

unread,
Apr 23, 2014, 3:34:54 AM4/23/14
to mi...@dartlang.org
And that was essentially what Florian said. I misread his answer :-)

Cheers,
Peter

Günter Zöchbauer

unread,
Apr 23, 2014, 6:04:03 AM4/23/14
to mi...@dartlang.org
Thanks for the response Florian and Peter!

I see now where I was mistaken.
the parens at `expectAsync( ... )` make it call and pass the result of course.

I interpreted it like it was written this way 

Timer.run(() => expectAsync(() { xxx }));

Cheers
Günter
Reply all
Reply to author
Forward
0 new messages