Getting Started
Architecture
NServiceBus
Transports
Persistence
ServiceInsight
ServicePulse
ServiceControl
Monitoring
Samples

Client-side Callbacks Testing

NuGet Package: NServiceBus.Callbacks.Testing (5.x)
Target Version: NServiceBus 9.x

Shows the usage of the NServiceBus.Callbacks.Testing package used for testing Callbacks.

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.

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<int>(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.

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<Status>(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.

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<ResponseMessage>(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.

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<ResponseMessage>(request, sendOptions);

Assert.AreEqual(simulatedResponse, result);

Cancellation

The asynchronous callback can be canceled by registering a CancellationToken provided by a CancellationTokenSource.

Testing

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<OperationCanceledException>(
    @delegate: async () =>
    {
        await session.Request<int>(request, tokenSource.Token);
    });

Samples

Related Articles