|
2 | 2 |
|
3 | 3 | # NServiceBus.IntegrationTesting
|
4 | 4 |
|
5 |
| -Experiments with NServiceBus integration testing , for more information refer to my [Exploring NServiceBus Integration Testing options](https://milestone.topics.it/2019/07/04/exploring-nservicebus-integration-testing-options.html) article. |
| 5 | +NServiceBus.IntegrationTesting allows testing end-to-end business scenarios, exercising the real production code. |
| 6 | + |
| 7 | +## tl;dr |
| 8 | + |
| 9 | +NServiceBus.IntegrationTesting enables a test like the following one to be defined: |
| 10 | + |
| 11 | +```csharp |
| 12 | +[Test] |
| 13 | +public async Task AReplyMessage_is_received_and_ASaga_is_started() |
| 14 | +{ |
| 15 | + var theExpectedSagaId = Guid.NewGuid(); |
| 16 | + var context = await Scenario.Define<IntegrationScenarioContext>() |
| 17 | + .WithEndpoint<MyServiceEndpoint>(g => g.When(b => b.Send(new AMessage() { ThisWillBeTheSagaId = theExpectedSagaId }))) |
| 18 | + .WithEndpoint<MyOtherServiceEndpoint>() |
| 19 | + .Done(c => |
| 20 | + { |
| 21 | + return |
| 22 | + ( |
| 23 | + c.HandlerWasInvoked<AMessageHandler>() |
| 24 | + && c.HandlerWasInvoked<AReplyMessageHandler>() |
| 25 | + && c.SagaWasInvoked<ASaga>() |
| 26 | + ) |
| 27 | + || c.HasFailedMessages(); |
| 28 | + }) |
| 29 | + .Run(); |
| 30 | + |
| 31 | + var invokedSaga = context.InvokedSagas.Single(s => s.SagaType == typeof(ASaga)); |
| 32 | + |
| 33 | + Assert.True(invokedSaga.IsNew); |
| 34 | + Assert.AreEqual("MyService", invokedSaga.EndpointName); |
| 35 | + Assert.True(((ASagaData)invokedSaga.SagaData).SomeId == theExpectedSagaId); |
| 36 | + Assert.False(context.HasFailedMessages()); |
| 37 | + Assert.False(context.HasHandlingErrors()); |
| 38 | +} |
| 39 | +``` |
| 40 | + |
| 41 | +(Full test [source code](https://github.com/mauroservienti/NServiceBus.IntegrationTesting/blob/master/src/MySystem.AcceptanceTests/When_sending_AMessage.cs) for the above sample is available in this repo) |
| 42 | + |
| 43 | +The above test does quite a lo of things, but the most important ones are: |
| 44 | + |
| 45 | +- it's exercising the real production endpoints |
| 46 | +- it's asserting on the end-to-end choreography, for example it's checking that a saga was invoked and/or a message handler was invoked |
| 47 | + |
| 48 | +When the test is started, it sends an initial `AMessage` message to trigger the choreography, and then it lets the endpoints involved do their job untill a specific condition is met. In this sample the `done` condition is quite complex: |
| 49 | + |
| 50 | +- An couple of handlers and a saga need to be invoked |
| 51 | +- Or there are failed messages |
| 52 | + |
| 53 | +*NOTE*: Endpoints in the samples contained in this repository are using RabbitMQ as the NServiceBus transport, LearningPersistence as the persistence mechanism. Tests are using docker-compose to make sure the required infrastructure is made available to endpoints exercised by tests. |
| 54 | + |
| 55 | +## How to install |
| 56 | + |
| 57 | +Using a package manager add a nuget reference to [NServiceBus.IntegrationTesting](https://www.nuget.org/packages/NServiceBus.IntegrationTesting/). |
| 58 | + |
| 59 | +## Background |
| 60 | + |
| 61 | +For more information on the genesis of this package refer to the [Exploring NServiceBus Integration Testing options](https://milestone.topics.it/2019/07/04/exploring-nservicebus-integration-testing-options.html) article. |
| 62 | + |
| 63 | +--- |
6 | 64 |
|
7 | 65 | Icon [test](https://thenounproject.com/search/?q=test&i=2829166) by Andrei Yushchenko from the Noun Project
|
0 commit comments