# Client-side Callbacks Testing Shows the usage of the `NServiceBus.Callbacks.Testing` package used for testing [Callbacks](callbacks.md). ## Prerequisites for callback testing functionality The approach shown here works with the `NServiceBus.Callbacks` NuGet package version 1.1.0 or above. Install the `NServiceBus.Callbacks.Testing` NuGet package. ### Int The integer response scenario allows any integer value to be returned in a strongly typed manner. #### Testing The response type returned by the `When` definition needs to be of type `int`. ```cs var request = new Message(); var simulatedResponse = 42; var session = new TestableCallbackAwareSession(); session.When( matcher: (Message message) => { return message == request; }, response: simulatedResponse); var result = await session.Request(request); Assert.AreEqual(simulatedResponse, result); ``` ### Enum The enum response scenario allows any enum value to be returned in a strongly typed manner. #### Testing The response type returned by the `When` definition needs to be of the enum type expected. ```cs var request = new Message(); var simulatedResponse = Status.OK; var session = new TestableCallbackAwareSession(); session.When( matcher: (Message message) => { return message == request; }, response: simulatedResponse); var result = await session.Request(request); Assert.AreEqual(simulatedResponse, result); ``` ### Object The object response scenario allows an object instance to be returned. #### Testing The response type returned by the `When` definition needs to be of the object response type expected. ```cs var request = new Message(); var simulatedResponse = new ResponseMessage(); var session = new TestableCallbackAwareSession(); session.When( matcher: (Message message) => { return message == request; }, response: simulatedResponse); var result = await session.Request(request); Assert.AreEqual(simulatedResponse, result); ``` #### Testing with SendOptions The `When` definition provides a matcher overload which allows matching against the response and the send options passed into the callback function. ```cs var request = new Message(); var simulatedResponse = new ResponseMessage(); var session = new TestableCallbackAwareSession(); session.When( matcher: (Message message, SendOptions options) => { return message == request && options.GetHeaders() .ContainsKey("Simulated.Header"); }, response: simulatedResponse); var sendOptions = new SendOptions(); sendOptions.SetHeader("Simulated.Header", "value"); var result = await session.Request(request, sendOptions); Assert.AreEqual(simulatedResponse, result); ``` ## Cancellation The asynchronous callback can be canceled by registering a `CancellationToken` provided by a `CancellationTokenSource`. #### Testing ```cs var tokenSource = new CancellationTokenSource(); var request = new Message(); var session = new TestableCallbackAwareSession(); session.When( matcher: (Message message) => { // When request matches, cancel the token source if (message == request) { tokenSource.Cancel(); } return false; }, response: 42); Assert.ThrowsAsync( @delegate: async () => { await session.Request(request, tokenSource.Token); }); ```