https://github.com/verifytests/verify.masstransit
Adds Verify support for MassTransit test helpers.
https://github.com/verifytests/verify.masstransit
Last synced: about 1 year ago
JSON representation
Adds Verify support for MassTransit test helpers.
- Host: GitHub
- URL: https://github.com/verifytests/verify.masstransit
- Owner: VerifyTests
- License: mit
- Created: 2022-02-25T10:16:25.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-10-21T03:51:10.000Z (over 1 year ago)
- Last Synced: 2024-10-21T06:50:56.163Z (over 1 year ago)
- Language: C#
- Homepage:
- Size: 373 KB
- Stars: 12
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- Funding: .github/FUNDING.yml
- License: license.txt
Awesome Lists containing this project
README
#
Verify.MassTransit
[](https://github.com/orgs/VerifyTests/discussions)
[](https://ci.appveyor.com/project/SimonCropp/Verify-MassTransit)
[](https://www.nuget.org/packages/Verify.MassTransit/)
Adds [Verify](https://github.com/VerifyTests/Verify) support for [MassTransit test helpers](https://masstransit-project.com/usage/testing.html).
**See [Milestones](../../milestones?state=closed) for release notes.**
## NuGet package
https://nuget.org/packages/Verify.MassTransit/
## Usage
```cs
[ModuleInitializer]
public static void Initialize() =>
VerifyMassTransit.Initialize();
```
snippet source | anchor
### Consumer Test
Using traditional asserts consumer interactions can be tested as follows:
```cs
[Fact]
public async Task TestWithAsserts()
{
using var harness = new InMemoryTestHarness();
var consumerHarness = harness.Consumer();
await harness.Start();
try
{
await harness.InputQueueSendEndpoint
.Send(
new SubmitOrder
{
OrderId = InVar.Id
});
// did the endpoint consume the message
Assert.True(await harness.Consumed.Any());
// did the actual consumer consume the message
Assert.True(await consumerHarness.Consumed.Any());
// the consumer publish the event
Assert.True(await harness.Published.Any());
// ensure that no faults were published by the consumer
Assert.False(await harness.Published.Any>());
}
finally
{
await harness.Stop();
}
}
```
snippet source | anchor
Using Verify, the TestHarness and any number of ConsumerHarness, can be passed to `Verify`.
```cs
[Fact]
public async Task TestWithVerify()
{
using var harness = new InMemoryTestHarness();
var consumer = harness.Consumer();
await harness.Start();
try
{
await harness.InputQueueSendEndpoint
.Send(
new SubmitOrder
{
OrderId = InVar.Id
});
await Verify(new
{
harness,
consumer
});
}
finally
{
await harness.Stop();
}
}
```
snippet source | anchor
The above will result in the following snapshot file that will need to be [accepted](https://github.com/VerifyTests/Verify#snapshot-management).
```txt
{
harness: {
Messages: [
{
Sent: ConsumerTests.SubmitOrder,
MessageId: Guid_1,
ConversationId: Guid_2,
DestinationAddress: input_queue,
Message: {
OrderId: Guid_3
}
},
{
Received: ConsumerTests.SubmitOrder,
MessageId: Guid_1,
ConversationId: Guid_2,
DestinationAddress: input_queue,
Message: {
OrderId: Guid_3
}
},
{
Published: ConsumerTests.OrderSubmitted,
MessageId: Guid_4,
ConversationId: Guid_2,
DestinationAddress: Tests:ConsumerTests+OrderSubmitted,
Message: {
OrderId: Guid_3
}
}
]
},
consumer: {
Consumed: [
{
Received: ConsumerTests.SubmitOrder,
MessageId: Guid_1,
ConversationId: Guid_2,
DestinationAddress: input_queue,
Message: {
OrderId: Guid_3
}
}
]
}
}
```
snippet source | anchor
Moving forward, any change in the message interactions will result in a new snapshot that can then be [accepted or declines](https://github.com/VerifyTests/Verify#snapshot-management)
### Saga Test
The following Saga test:
```cs
[Fact]
public async Task Run()
{
using var harness = new InMemoryTestHarness();
var sagaHarness = harness.Saga();
var correlationId = NewId.NextGuid();
await harness.Start();
try
{
await harness.Bus.Publish(new Start {CorrelationId = correlationId});
await harness.Consumed.Any();
await Verify(new {harness, sagaHarness});
}
finally
{
await harness.Stop();
}
}
public class ConsumerSaga :
ISaga,
InitiatedBy
{
public Guid CorrelationId { get; set; }
public bool StartMessageReceived { get; set; }
public Task Consume(ConsumeContext context)
{
StartMessageReceived = true;
return Task.CompletedTask;
}
}
public class Start : CorrelatedBy
{
public Guid CorrelationId { get; init; }
}
```
snippet source | anchor
Will result in the following snapshot file.
```txt
{
harness: {
Messages: [
{
Published: SagaTests.Start,
MessageId: Guid_1,
ConversationId: Guid_2,
DestinationAddress: Tests:SagaTests+Start,
Message: {
CorrelationId: Guid_3
}
},
{
Received: SagaTests.Start,
MessageId: Guid_1,
ConversationId: Guid_2,
DestinationAddress: Tests:SagaTests+Start,
Message: {
CorrelationId: Guid_3
}
}
]
},
sagaHarness: {
Consumed: [
{
Received: SagaTests.Start,
MessageId: Guid_1,
ConversationId: Guid_2,
DestinationAddress: Tests:SagaTests+Start,
Message: {
CorrelationId: Guid_3
}
}
],
Sagas: [
{
Saga: {
CorrelationId: Guid_3,
StartMessageReceived: true
},
ElementId: Guid_3
}
]
}
}
```
snippet source | anchor
## Icon
[Approval](https://thenounproject.com/term/bus/4628287/) designed by [SAM Designs](https://thenounproject.com/ma2947422/) from [The Noun Project](https://thenounproject.com/).