Skip to content

Commit

Permalink
chore(deps): replace Moq with NSubstitute. (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
skwasjer committed Nov 25, 2023
1 parent 26f7e36 commit 4cc4ec4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 49 deletions.
4 changes: 2 additions & 2 deletions test/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

<ItemGroup>
<Using Include="FluentAssertions" />
<Using Include="Moq" />
<Using Include="NSubstitute" />
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="NSubstitute" Version="5.1.0" />
<PackageReference Include="xunit" Version="2.6.2" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,29 @@ namespace Rebus.Correlate.Steps;

public class CorrelateIncomingMessageStepTests
{
private readonly Mock<IAsyncCorrelationManager> _asyncCorrelationManagerMock;
private readonly Dictionary<string, string> _messageHeaders;
private readonly IAsyncCorrelationManager _asyncCorrelationManagerMock;
private readonly Dictionary<string, string> _messageHeaders = new();
private readonly IncomingStepContext _stepContext;
private readonly Func<Task> _next;
private readonly CorrelateIncomingMessageStep _sut;

public CorrelateIncomingMessageStepTests()
{
_asyncCorrelationManagerMock = new Mock<IAsyncCorrelationManager>();
_asyncCorrelationManagerMock = Substitute.For<IAsyncCorrelationManager>();

var txItems = new ConcurrentDictionary<string, object>();
var transactionContextMock = new Mock<ITransactionContext>();
ITransactionContext transactionContextMock = Substitute.For<ITransactionContext>();
transactionContextMock
.Setup(m => m.Items)
.Items
.Returns(txItems);

_messageHeaders = new Dictionary<string, string>();
var transportMessage = new TransportMessage(_messageHeaders, Array.Empty<byte>());
_stepContext = new IncomingStepContext(transportMessage, transactionContextMock.Object);
_stepContext = new IncomingStepContext(transportMessage, transactionContextMock);
_stepContext.Save(new Message(_messageHeaders, new { }));

_next = () => Task.CompletedTask;

_sut = new CorrelateIncomingMessageStep(_asyncCorrelationManagerMock.Object, new NullLoggerFactory());
_sut = new CorrelateIncomingMessageStep(_asyncCorrelationManagerMock, new NullLoggerFactory());
}

[Fact]
Expand All @@ -55,7 +54,7 @@ public void When_creating_instance_without_loggerFactory_it_should_not_throw()
IRebusLoggerFactory? rebusLoggerFactory = null;

// Act
Func<CorrelateIncomingMessageStep> act = () => new CorrelateIncomingMessageStep(_asyncCorrelationManagerMock.Object, rebusLoggerFactory);
Func<CorrelateIncomingMessageStep> act = () => new CorrelateIncomingMessageStep(_asyncCorrelationManagerMock, rebusLoggerFactory);

// Assert
act.Should().NotThrow();
Expand All @@ -71,7 +70,9 @@ public async Task Given_no_correlation_id_is_stored_with_message_it_should_use_m
await _sut.Process(_stepContext, _next);

// Assert
_asyncCorrelationManagerMock.Verify(m => m.CorrelateAsync(expectedCorrelationId, _next, It.IsAny<OnException>()), Times.Once);
await _asyncCorrelationManagerMock
.Received(1)
.CorrelateAsync(expectedCorrelationId, _next, Arg.Any<OnException>());
}

[Fact]
Expand All @@ -85,6 +86,8 @@ public async Task Given_correlation_id_is_stored_with_message_it_should_use_head
await _sut.Process(_stepContext, _next);

// Assert
_asyncCorrelationManagerMock.Verify(m => m.CorrelateAsync(expectedCorrelationId, _next, It.IsAny<OnException>()), Times.Once);
await _asyncCorrelationManagerMock
.Received(1)
.CorrelateAsync(expectedCorrelationId, _next, Arg.Any<OnException>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,29 @@ namespace Rebus.Correlate.Steps;
public class CorrelateOutgoingMessageStepTests
{
private readonly CorrelationContextAccessor _correlationContextAccessor;
private readonly Mock<ICorrelationIdFactory> _correlationIdFactoryMock;
private readonly Mock<ITransactionContext> _transactionContextMock;
private readonly Dictionary<string, string> _messageHeaders;
private readonly ICorrelationIdFactory _correlationIdFactoryMock;
private readonly ITransactionContext _transactionContextMock;
private readonly Dictionary<string, string> _messageHeaders = new();
private readonly OutgoingStepContext _stepContext;
private readonly Func<Task> _next;
private readonly CorrelateOutgoingMessageStep _sut;

public CorrelateOutgoingMessageStepTests()
{
_correlationContextAccessor = new CorrelationContextAccessor();
_correlationIdFactoryMock = new Mock<ICorrelationIdFactory>();
_correlationIdFactoryMock = Substitute.For<ICorrelationIdFactory>();

var txItems = new ConcurrentDictionary<string, object>();
_transactionContextMock = new Mock<ITransactionContext>();
_transactionContextMock = Substitute.For<ITransactionContext>();
_transactionContextMock
.Setup(m => m.Items)
.Items
.Returns(txItems);

_messageHeaders = new Dictionary<string, string>();
_stepContext = new OutgoingStepContext(new Message(_messageHeaders, new { }), _transactionContextMock.Object, new DestinationAddresses(Enumerable.Empty<string>()));
_stepContext = new OutgoingStepContext(new Message(_messageHeaders, new { }), _transactionContextMock, new DestinationAddresses(Enumerable.Empty<string>()));

_next = () => Task.CompletedTask;

_sut = new CorrelateOutgoingMessageStep(_correlationContextAccessor, _correlationIdFactoryMock.Object, new NullLoggerFactory());
_sut = new CorrelateOutgoingMessageStep(_correlationContextAccessor, _correlationIdFactoryMock, new NullLoggerFactory());
}

[Fact]
Expand All @@ -43,7 +42,7 @@ public void When_creating_instance_without_correlationContextAccessor_it_should_
ICorrelationContextAccessor? correlationContextAccessor = null;

// Act
Func<CorrelateOutgoingMessageStep> act = () => new CorrelateOutgoingMessageStep(correlationContextAccessor!, _correlationIdFactoryMock.Object, new NullLoggerFactory());
Func<CorrelateOutgoingMessageStep> act = () => new CorrelateOutgoingMessageStep(correlationContextAccessor!, _correlationIdFactoryMock, new NullLoggerFactory());

// Assert
act.Should()
Expand All @@ -57,7 +56,7 @@ public void When_creating_instance_without_loggerFactory_it_should_not_throw()
IRebusLoggerFactory? rebusLoggerFactory = null;

// Act
Func<CorrelateOutgoingMessageStep> act = () => new CorrelateOutgoingMessageStep(_correlationContextAccessor, _correlationIdFactoryMock.Object, rebusLoggerFactory);
Func<CorrelateOutgoingMessageStep> act = () => new CorrelateOutgoingMessageStep(_correlationContextAccessor, _correlationIdFactoryMock, rebusLoggerFactory);

// Assert
act.Should().NotThrow();
Expand Down Expand Up @@ -87,12 +86,6 @@ public async Task Given_message_already_has_a_correlation_id_stored_it_should_ig

bool isNextCalled = false;

Task Next()
{
isNextCalled = true;
return Task.CompletedTask;
}

// Act
await _sut.Process(_stepContext, Next);

Expand All @@ -103,8 +96,15 @@ Task Next()
.WhoseValue
.Should()
.Be(expectedCorrelationId);
_correlationIdFactoryMock.Verify(m => m.Create(), Times.Never);
_correlationIdFactoryMock.DidNotReceive().Create();
isNextCalled.Should().BeTrue();
return;

Task Next()
{
isNextCalled = true;
return Task.CompletedTask;
}
}

[Fact]
Expand All @@ -117,12 +117,6 @@ public async Task Given_message_has_no_correlation_id_and_correlation_context_is

bool isNextCalled = false;

Task Next()
{
isNextCalled = true;
return Task.CompletedTask;
}

// Act
await _sut.Process(_stepContext, Next);

Expand All @@ -133,8 +127,15 @@ Task Next()
.WhoseValue
.Should()
.Be(expectedCorrelationId);
_correlationIdFactoryMock.Verify(m => m.Create(), Times.Never);
_correlationIdFactoryMock.DidNotReceive().Create();
isNextCalled.Should().BeTrue();
return;

Task Next()
{
isNextCalled = true;
return Task.CompletedTask;
}
}

[Fact]
Expand All @@ -145,18 +146,11 @@ public async Task Given_message_has_no_correlation_id_and_correlation_context_is
_messageHeaders.Clear();
_correlationContextAccessor.CorrelationContext = null;
_correlationIdFactoryMock
.Setup(m => m.Create())
.Returns(correlationId)
.Verifiable();
.Create()
.Returns(correlationId);

bool isNextCalled = false;

Task Next()
{
isNextCalled = true;
return Task.CompletedTask;
}

// Act
await _sut.Process(_stepContext, Next);

Expand All @@ -167,8 +161,15 @@ Task Next()
.WhoseValue
.Should()
.Be(expectedCorrelationId);
_correlationIdFactoryMock.Verify();
_correlationIdFactoryMock.Received(1).Create();
isNextCalled.Should().BeTrue();
return;

Task Next()
{
isNextCalled = true;
return Task.CompletedTask;
}
}

[Fact]
Expand Down Expand Up @@ -203,7 +204,7 @@ public async Task Given_incoming_step_context_has_sequence_it_should_increment(i
new TransportMessage(
incomingHeaders,
Array.Empty<byte>()),
_transactionContextMock.Object
_transactionContextMock
);
incomingStepContext.Save(new Message(incomingHeaders, new { }));
_stepContext.Save(incomingStepContext);
Expand Down

0 comments on commit 4cc4ec4

Please sign in to comment.