This is part of the NServiceBus Upgrade Guide from Version 5 to 6, which also includes the following individual upgrade guides for specific components:
Feature Details
- Assembly Scanning Changes in NServiceBus Version 6
- No Async Suffix
- Dependency Injection Changes in NServiceBus Version 6
- Deprecated TransportMessage in NServiceBus Version 6
- Endpoint API changes in NServiceBus Version 6
- Extension Seam Changes in NServiceBus Version 6
- Migrate handlers and sagas to Version 6
- Header API changes in NServiceBus Version 6
- Messaging Changes in NServiceBus Version 6
- Moving away from IBus in Version 6
- Recoverability Changes in Version 6
- Serialization Changes in NServiceBus Version 6
- Subscription Changes in NServiceBus Version 6
- Transaction Configuration Changes in NServiceBus Version 6
Transports
- Azure Service Bus Transport (Legacy) Upgrade Version 6 to 7
- RabbitMQ Transport Upgrade Version 3 to 4
- SQL Server Transport Upgrade Version 2 to 3
- SQL Server Transport Upgrade - Supporting Unicode in Headers
Persistence
- Upgrade from NServiceBus Azure Version 6
- NHibernate Persistence Upgrade Version 6 to 7
- NHibernate Persistence - Resolving incorrect timeout table indexes
- RavenDB Persistence Upgrade from 3 to 4
Hosting
Other
- Moving to the DataBus AzureBlobStorage Package
- Azure Cloud Services Host Upgrade Version 6 to 7
- NServiceBus.Azure package deprecated
- Gateway Upgrade Version 1 to 2
- NServiceBus Testing Upgrade Version 5 to 6
- Callback Changes in NServiceBus Version 6
- Migrating the distributor to use sender-side distribution
- Tool and Helper Changes in NServiceBus Version 6
NServiceBus.Testing requires NServiceBus version 6.
As part of upgrading to NServiceBus.Testing version 6, projects will also require an upgrade to NServiceBus version 6.
New unit testing capabilities
NServiceBus.Testing version 6 provides an alternate way to write tests using a more traditional Arrange Act Assert pattern. This allows for easier extension and tooling integration. For more details on the new testing capabilities, see the Unit Testing NServiceBus 6 Sample.
Testing framework
Removed Test.Initialize()
It is no longer necessary to call Test.
before executing a test. All calls to Test.
can be safely removed.
Unobtrusive message conventions
With the removal of Test.
, it is also no longer necessary to configure unobtrusive message conventions.
Testing message handlers
ExpectReturn method
Use ExpectReply
instead of ExpectReturn
.
SendToSites methods
ExpectSendToSites
and ExpectNotSendToSites
methods have been removed from the testing framework. Handlers using the gateway can still be tested using the ExpectSend
overload which provides the SendOption
:
// For Testing version 7.x
[Test]
public void ExpectSendToSite()
{
Test.Handler<MyHandler>()
.ExpectSend<GatewayMessage>(
check: (message, options) =>
{
return options.GetSitesRoutingTo().Contains("myFavouriteSite");
})
.OnMessage(new MyMessage());
}
class MyHandler :
IHandleMessages<MyMessage>
{
public Task Handle(MyMessage message, IMessageHandlerContext context)
{
var options = new SendOptions();
options.RouteToSites("myFavoriteSite");
return context.Send(new GatewayMessage(), options);
}
}
// For Testing version 6.x
[Test]
public void ExpectSendToSite()
{
Test.Handler<MyHandler>()
.ExpectSend<GatewayMessage>(
check: (message, options) =>
{
return options.GetSitesRoutingTo().Contains("myFavouriteSite");
})
.OnMessage(new MyMessage());
}
class MyHandler :
IHandleMessages<MyMessage>
{
public Task Handle(MyMessage message, IMessageHandlerContext context)
{
var options = new SendOptions();
options.RouteToSites("myFavoriteSite");
return context.Send(new GatewayMessage(), options);
}
}
Testing sagas
Using When
In NServiceBus version 6 and above, message handlers have an additional IMessageHandlerContext
parameter. This context parameter must be provided when defining the method to invoke.
.When(
sagaIsInvoked: (saga, context) =>
{
return saga.Handle(new StartsSaga(), context);
})
A new overload has been added to simplify this:
.When(
handlerSelector: saga =>
{
return saga.Handle;
},
message: new StartsSaga())
It's important to pass the context provided by the delegate arguments to the handle method.
Configuring a message ID
The message ID can be configured manually using the ConfigureMessageHandler
option. See Configuring the context section below for more details.
Configuring the context
Both saga and handler tests contain a ConfigureHandlerContext
method to enable custom configuration of the IMessageHandlerContext
which is passed to the invoked handler methods.
// For Testing version 7.x
Test.Saga<MySaga>()
.ConfigureHandlerContext(c =>
{
c.MessageId = "my message ID";
})
.ExpectPublish<MyEvent>()
.When(s => s.Handle, new StartsSaga());
// For Testing version 6.x
Test.Saga<MySaga>()
.ConfigureHandlerContext(c =>
{
c.MessageId = "my message ID";
})
.ExpectPublish<MyEvent>()
.When(s => s.Handle, new StartsSaga());