-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from KyleMcMaster/feature/saga-unit-testing
Add Saga Unit Tests
- Loading branch information
Showing
4 changed files
with
89 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
tests/NServiceBusTutorial.UnitTests/ContributorVerificationSagaTests/SagaScenarioTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using FluentAssertions; | ||
using FluentAssertions.Execution; | ||
using NServiceBus.Testing; | ||
using NServiceBusTutorial.Core.ContributorAggregate.Commands; | ||
using NServiceBusTutorial.Core.ContributorAggregate.Events; | ||
using NServiceBusTutorial.Saga; | ||
using Xunit; | ||
|
||
namespace NServiceBusTutorial.UnitTests.ContributorVerificationSagaTests; | ||
|
||
public class SagaScenarioTests | ||
{ | ||
[Fact] | ||
public async Task ShouldInitializeSagaAndMarkAsCompleted() | ||
{ | ||
int expectedContributorId = 4680; | ||
var startCommand = new StartContributorVerificationCommand | ||
{ | ||
ContributorId = expectedContributorId | ||
}; | ||
var verifiedEvent = new ContributorVerifiedEvent | ||
{ | ||
ContributorId = expectedContributorId | ||
}; | ||
var saga = new TestableSaga<ContributorVerificationSaga, ContributorVerificationSagaData>(); | ||
var context = new TestableMessageHandlerContext(); | ||
|
||
var startResult = await saga.Handle(startCommand, context); | ||
var completeResult = await saga.Handle(verifiedEvent, context); | ||
|
||
using var assertionScope = new AssertionScope(); | ||
startResult.SagaDataSnapshot.ContributorId.Should().Be(expectedContributorId); | ||
completeResult.Completed.Should().BeTrue(); | ||
} | ||
|
||
[Fact] | ||
public async Task ShouldTimeoutWhenTimeAdvancesOverLimit() | ||
{ | ||
int expectedContributorId = 4680; | ||
var startCommand = new StartContributorVerificationCommand | ||
{ | ||
ContributorId = expectedContributorId | ||
}; | ||
var saga = new TestableSaga<ContributorVerificationSaga, ContributorVerificationSagaData>(); | ||
var context = new TestableMessageHandlerContext(); | ||
|
||
var startResult = await saga.Handle(startCommand, context); | ||
var timeouts = await saga.AdvanceTime(TimeSpan.FromHours(25)); | ||
|
||
using var assertionScope = new AssertionScope(); | ||
startResult.SagaDataSnapshot.ContributorId.Should().Be(expectedContributorId); | ||
var timeoutResult = timeouts.Single(); | ||
timeoutResult.FindSentMessage<NotVerifyContributorCommand>().Should().NotBeNull(); | ||
timeoutResult.Completed.Should().BeTrue(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...al.UnitTests/ContributorVerificationSagaTests/StartContributorVerificationCommandTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using FluentAssertions; | ||
using NServiceBus.Testing; | ||
using NServiceBusTutorial.Core.ContributorAggregate.Commands; | ||
using NServiceBusTutorial.Saga; | ||
using Xunit; | ||
|
||
namespace NServiceBusTutorial.UnitTests.ContributorVerificationSagaTests; | ||
|
||
public class StartContributorVerificationCommandTests | ||
{ | ||
[Fact] | ||
public async Task ShouldSendVerifyContributorCommand() | ||
{ | ||
var message = new StartContributorVerificationCommand(); | ||
var saga = new ContributorVerificationSaga | ||
{ | ||
Data = new() | ||
}; | ||
var context = new TestableMessageHandlerContext(); | ||
|
||
await saga.Handle(message, context); | ||
|
||
var sentMessage = context.FindSentMessage<VerifyContributorCommand>(); | ||
sentMessage.Should().NotBeNull(); | ||
} | ||
} |