Testkit
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 https://github.com/stretchr/testify
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)
wherectx
is a go context andt
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)
wherectx
is a go context. One can set some optionsUse the probe to send a message to the actor under test. Example:
probe.Send(pinger, new(testpb.Ping))
for a Tell assertion andprobe.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 oneExpectMessageWithin(duration time.Duration, message proto.Message)
: asserts that the message received from the test actor is the expected one within a time durationExpectNoMessage()
: asserts that no message is expectedExpectAnyMessage() proto.Message
: asserts that any message is expectedExpectAnyMessageWithin(duration time.Duration) proto.Message
: asserts that any message within a time durationExpectMessageOfType(messageType protoreflect.MessageType)
: asserts the expectation of a given message typeExpectMessageOfTypeWithin(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)
wherectx
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 Testkit
Last updated