Skip to content

Commit

Permalink
Merge pull request #5 from KyleMcMaster/feature/saga-unit-testing
Browse files Browse the repository at this point in the history
Add Saga Unit Tests
  • Loading branch information
KyleMcMaster authored Sep 27, 2024
2 parents fa5b82b + 714ff96 commit 05e0bc2
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 31 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,10 @@ csharp_style_expression_bodied_local_functions = false:silent
###############################
# VB Coding Conventions #
###############################

# IDE0005: Using directive is unnecessary.
dotnet_diagnostic.IDE0005.severity = warning

[*.vb]
# Modifier preferences
visual_basic_preferred_modifier_order = Partial,Default,Private,Protected,Public,Friend,NotOverridable,Overridable,MustOverride,Overloads,Overrides,MustInherit,NotInheritable,Static,Shared,Shadows,ReadOnly,WriteOnly,Dim,Const,WithEvents,Widening,Narrowing,Custom,Async:suggestion
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using FluentAssertions;
using FluentAssertions.Execution;
using NServiceBus.Testing;
using NServiceBusTutorial.Core.ContributorAggregate;
using NServiceBusTutorial.Core.ContributorAggregate.Commands;
using NServiceBusTutorial.Core.ContributorAggregate.Events;
using NServiceBusTutorial.Saga;
using Xunit;
Expand All @@ -14,36 +11,11 @@ public class ContributorVerifiedEventTests
public async Task ShouldMarkSagaAsCompleted()
{
var message = new ContributorVerifiedEvent();
var saga = new TestableSaga<ContributorVerificationSaga, ContributorVerificationSagaData>();
var saga = new ContributorVerificationSaga();
var context = new TestableMessageHandlerContext();

var result = await saga.Handle(message, context);
await saga.Handle(message, context);

result.Completed.Should().BeTrue();
}

[Fact]
public async Task ShouldInitializeSagaAndMarkAsCompleted()
{
int expectedContributorId = 1;
var startMessage = new StartContributorVerificationCommand()
{
ContributorId = expectedContributorId
};
var message = new ContributorVerifiedEvent()
{
ContributorId = expectedContributorId
};
var saga = new TestableSaga<ContributorVerificationSaga, ContributorVerificationSagaData>();
var context = new TestableMessageHandlerContext();

var result = await saga.Handle(startMessage, context);
using var assertionScope = new AssertionScope();
result.SagaDataSnapshot.ContributorId.Should().Be(expectedContributorId);

result = await saga.Handle(message, context);
result.Completed.Should().BeTrue();
var timeoutMessage = result.FindTimeoutMessage<ContributorVerificationSagaTimeout>();
timeoutMessage.Should().NotBeNull();
saga.Completed.Should().BeTrue();
}
}
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();
}
}
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();
}
}

0 comments on commit 05e0bc2

Please sign in to comment.