dactor_test 2.0.0
dactor_test: ^2.0.0 copied to clipboard
Test utilities for the Dactor actor system - TestActorSystem and TestProbe
dactor_test #
Test utilities for the Dactor actor system.
- TestActorSystem: a
LocalActorSystemthat creates probes. - TestProbe: an actor that sends messages on a test's behalf and asserts what comes back.
Version 2.x is for dactor 2.x; use 1.x with dactor 1.x.
Usage #
import 'package:dactor/dactor.dart';
import 'package:dactor_test/dactor_test.dart';
import 'package:test/test.dart';
class Echo extends Actor {
@override
Future<void> onMessage(dynamic message) async {
context.sender?.tell('echo: $message');
}
}
void main() {
late TestActorSystem system;
setUp(() => system = TestActorSystem());
tearDown(() => system.shutdown());
test('echoes to the sender', () async {
final probe = await system.createProbe();
final echo = await system.spawn('echo', () => Echo());
// The probe is the sender, so the reply comes back to it.
probe.send(echo, 'hello');
await probe.expectMsg('echo: hello', timeout: const Duration(seconds: 1));
});
}
TestProbe #
| Member | What it does |
|---|---|
send(target, message) |
Tells target the message, with the probe as the sender |
expectMsg(expected, {timeout}) |
Waits for the next message and checks it equals (or matches) expected |
expectMsgType<T>({timeout}) |
Waits for the next message, checks it is a T and returns it |
reply(message) |
Tells the sender of the last message received |
watch(target) |
The probe receives a Terminated when target ends |
lastMessage |
The last message received |
ref |
The probe's own ActorRef |
A message that arrives while no expectMsg or expectMsgType is waiting is kept only as lastMessage. Call expectMsg before awaiting anything else that could let the message arrive first, as in the example above.
License #
MIT