e05486f88f | ||
---|---|---|
.github | ||
src | ||
.gitignore | ||
license.txt | ||
readme.md |
readme.md
Verify.NServiceBus
Adds Verify support to verify NServiceBus.
See Milestones for release notes.
NuGet package
https://nuget.org/packages/Verify.NServiceBus/
Usage
[ModuleInitializer]
public static void Initialize() =>
VerifyNServiceBus.Initialize();
Verifying a context
Given the following handler:
public class MyHandler :
IHandleMessages<MyRequest>
{
public async Task Handle(MyRequest message, HandlerContext context)
{
await context.Publish(
new MyPublishMessage
{
Property = "Value"
});
await context.Reply(
new MyReplyMessage
{
Property = "Value"
});
var sendOptions = new SendOptions();
sendOptions.DelayDeliveryWith(TimeSpan.FromHours(12));
await context.Send(
new MySendMessage
{
Property = "Value"
},
sendOptions);
await context.ForwardCurrentMessageTo("newDestination");
}
}
The test that verifies the resulting context:
[Fact]
public async Task VerifyHandlerResult()
{
var handler = new MyHandler();
var context = new RecordingHandlerContext();
var message = new MyRequest();
await handler.Handle(message, context);
await Verify(context);
}
The resulting verification file is as follows:
{
Forward: [
newDestination
],
Publish: [
{
MyPublishMessage: {
Property: Value
}
}
],
Reply: [
{
MyReplyMessage: {
Property: Value
}
}
],
Send: [
{
MySendMessage: {
Property: Value
},
Options: {
DeliveryDelay: 12:00:00
}
}
]
}
Recording
Recording allows all message interaction with the test context to be captured and then verified.
[Fact]
public async Task VerifyHandlerResult()
{
Recording.Start();
var handler = new MyHandler();
var context = new RecordingHandlerContext();
var message = new MyRequest();
await handler.Handle(message, context);
await Verify("some other data");
}
The resulting context verification file is as follows:
{
target: some other data,
message: [
{
Publish: {
MyPublishMessage: {
Property: Value
}
}
},
{
Reply: {
MyReplyMessage: {
Property: Value
}
}
},
{
Send: {
MySendMessage: {
Property: Value
},
Options: {
DeliveryDelay: 12:00:00
}
}
}
]
}
Verifying a Saga
Given the following handler:
public class MySaga :
Saga<MySaga.MySagaData>,
IHandleMessages<MyRequest>
{
protected override void ConfigureHowToFindSaga(SagaPropertyMapper<MySagaData> mapper) =>
mapper.ConfigureMapping<MyRequest>(message => message.OrderId)
.ToSaga(sagaData => sagaData.OrderId);
public async Task Handle(MyRequest message, HandlerContext context)
{
await context.Publish(
new MyPublishMessage
{
Property = "Value"
});
Data.MessageCount++;
}
public class MySagaData :
ContainSagaData
{
public Guid OrderId { get; set; }
public int MessageCount { get; set; }
}
}
The test that verifies the resulting context:
[Fact]
public async Task VerifySagaResult()
{
var saga = new MySaga
{
Data = new()
};
var context = new RecordingHandlerContext();
var message = new MyRequest();
await saga.Handle(message, context);
await Verify(new
{
context,
saga
});
}
The resulting verification file is as follows:
{
context: {
Publish: [
{
MyPublishMessage: {
Property: Value
}
}
]
},
saga: {
MessageCount: 1
}
}
Example behavior change
The next time there is a code change, that results in a different resulting interactions with NServiceBus, those changes can be visualized. For example if the DelayDeliveryWith
is changed from 12 hours to 1 day:
await context.Publish(
new MyPublishMessage
{
Property = "Value"
});
await context.Reply(
new MyReplyMessage
{
Property = "Value"
});
var sendOptions = new SendOptions();
sendOptions.DelayDeliveryWith(TimeSpan.FromDays(1));
await context.Send(
new MySendMessage
{
Property = "Value"
},
sendOptions);
await context.ForwardCurrentMessageTo("newDestination");
Then the resulting visualization diff would look as follows:
Message to Handler mapping
MessageToHandlerMap
allows verification of message that do not have a handler.
For example:
var map = new MessageToHandlerMap();
map.AddMessagesFromAssembly<MyMessage>();
map.AddHandlersFromAssembly<MyHandler>();
await Verify(map);
Would result in:
{
MessagesWithNoHandler: [
MessageToHandlerMapTests.MessageWithNoHandler
]
}
Icon
Approval designed by Mike Zuidgeest from The Noun Project.