Callbacks
Describes the possible expectations for working with callbacks.
| Expectation | Negated | Summary |
|---|---|---|
Signaled | DidNotSignal | the callback was signaled (a number of times) |
Signaler
First, you have to start recording callback signals using the Signaler class. This class is available in the
aweXpect.Signaling namespace:
using aweXpect.Signaling;
// ↓ Counts signals from callbacks without parameters
Signaler signaler = new();
Signaler<string> titleSignaler = new();
// ↑ Counts signals from callbacks with a string parameter
Then, you can signal the callback on the recording:
class Player
{
public void Play(string title, Action<string> onCompleted)
{
// play the track in a background thread and then call the onCompleted callback
}
}
player.Play("Let It Be", title => titleSignaler.Signal(title));
At last, you can wait for the callback to be signaled:
await Expect.That(titleSignaler).Signaled();
You can also verify that the callback will not be signaled:
await Expect.That(titleSignaler).DidNotSignal();
Without a limit, DidNotSignal() would never return. So when no timeout is specified, it waits for the default
timeout of 30 seconds, which the failure message shows (e.g. "within 0:30"). Use Within(…) to wait for a shorter
time, see waiting for callbacks.
Amount
You can specify how often a callback must be signaled:
using aweXpect.Core; // for `Times()`
await Expect.That(signaler).Signaled().AtLeast(3.Times());
await Expect.That(signaler).Signaled().Exactly(3.Times());
await Expect.That(signaler).Signaled().AtMost(3.Times());
await Expect.That(signaler).Signaled().LessThan(3.Times());
await Expect.That(signaler).Signaled().Between(2).And(4.Times());
await Expect.That(signaler).Signaled().Once();
await Expect.That(signaler).Signaled().Twice();
await Expect.That(signaler).Signaled().Never();
Signaled(3.Times()) and DidNotSignal(3.Times()) are shorthands for the AtLeast form. Only expectations without
an upper bound (e.g. AtLeast) can complete as soon as enough callbacks were signaled, all others wait for the
timeout.
Parameters
You can filter for signals with specific parameters by providing a predicate:
Signaler<string> signaler = new();
signaler.Signal("Yesterday");
signaler.Signal("Let It Be");
signaler.Signal("Yesterday");
await Expect.That(signaler).Signaled().AtLeast(2.Times()).With(p => p == "Yesterday");
WhoseParameters continues with expectations on the collection of recorded parameters:
Signaler<string> signaler = new();
signaler.Signal("Yesterday");
signaler.Signal("Let It Be");
await Expect.That(signaler).Signaled().AtLeast(2.Times()).WhoseParameters.Contains("Let It Be");
A failure message lists the recorded parameters.