Testkit
Last updated
Last updated
GoAkt comes packaged with a testkit that can help test that actors receive expected messages within unit tests. The teskit in GoAkt uses underneath the package. To test that an actor receive and respond to messages one will have to:
Create an instance of the testkit: testkit := New(ctx, t)
where ctx
is a go context and t
the instance of *testing.T
. This can be done in setup before the run of each test.
Create the instance of the actor under test. Example: pinger := testkit.Spawn(ctx, "pinger", &pinger{})
Create an instance of test probe: probe := testkit.NewProbe(ctx)
where ctx
is a go context. One can set some
Use the probe to send a message to the actor under test. Example: probe.Send(pinger, new(testpb.Ping))
for a Tell assertion and probe.SendSync(pinger, new(testpb.Ping), time.Second)
for an Ask assertion.
Assert that the actor under test has received the message and responded as expected using the probe methods:
ExpectMessage(message proto.Message)
: asserts that the message received from the test actor is the expected one
ExpectMessageWithin(duration time.Duration, message proto.Message)
: asserts that the message received from the test actor is the expected one within a time duration
ExpectNoMessage()
: asserts that no message is expected
ExpectAnyMessage() proto.Message
: asserts that any message is expected
ExpectAnyMessageWithin(duration time.Duration) proto.Message
: asserts that any message within a time duration
ExpectMessageOfType(messageType protoreflect.MessageType)
: asserts the expectation of a given message type
ExpectMessageOfTypeWithin(duration time.Duration, messageType protoreflect.MessageType)
: asserts the expectation of a given message type within a time duration
Make sure to shut down the testkit and the probe. Example: probe.Stop()
, testkit.Shutdown(ctx)
where ctx
is a go context. These two calls can be in a tear down after all tests run.
To help implement unit tests in GoAkt-based applications. See