If your stream is emitting objects with properties you'd like to test, expectAsync1
can help:
List<Record> expectedRecords = [record1, record2, record3];
int i = 0;
recordStream.listen(
expectAsync1<void,Record>(
(record) {
expect(record.name, expectedRecords[i].name);
i++;
},
max: -1)
);
In the above example, expectAsync1
encloses an anonymous function:
(record) {
expect(record.name, expectedRecords[i].name);
i++;
}
This gets run each time a Record
is emitted by the Stream recordStream
The 1
in expectAsync1
is the number of arguments your enclosed function will take. Most often, this would be 1. (record)
is the one argument above.
For the above example, expectAsync1
has (optional) type arguments: <void,Record>
The 2nd type argument Record
tells the enclosed function that the stream item emitted is of Record
type, allowing me to use properties like record.name
without casting.
The 1st type argument is the return type of your enclosed function. I used void
cause the enclosed function isn't returning anything, it's just running an expect
Matcher and iterating a counter, which is used to iterate through the list of Record
I'm expecting to see (i.e. expectedRecords
) in that order.
Max argument
You'll notice the max: -1
below the enclosed function. That's an optional but important argument for expectAsync1
specifying the number of stream items/events we're expecting.
This defaults to 1
if max
is not given and your test will fail if more than 1 event is emitted.
The error will be Callback called more times than expected (1).
In the example above I used -1
wich means unlimited events can be emitted/tested. You can specify a non-zero number if you want to test you get exactly that many items/events from your stream, else the test will fail. I could have used max: 3
for my example above.
RxDart
If you're using RxDart BehaviorSubject
remember the most recent stream event is emitted upon listen
. So in your test, when you start listening / using expectAsync1
there will be an immediate call of the enclosed function with the most recent event.
ReplaySubject
will emit all previous stream events upon listen
.