Skip to content

Commit

Permalink
test: Replace Moq with NSubstitute
Browse files Browse the repository at this point in the history
  • Loading branch information
oboukli committed Aug 11, 2023
1 parent f816a8d commit e2b9ae5
Show file tree
Hide file tree
Showing 18 changed files with 247 additions and 233 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ and ideas leveraged by this project:
- MediatR
- Mermaid
- Modern code style
- moq
- NServiceBus
- NSubstitute
- Open Library API
- OpenAPI/Swaggar
- OpenTelemetry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3"/>
<PackageReference Include="Moq" Version="4.18.4"/>
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="NSubstitute.Analyzers.CSharp" Version="1.0.16">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.5.0"/>
<PackageReference Include="xunit.analyzers" Version="1.2.0"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,31 @@
using Aktabook.Application.Messages.Commands;
using Aktabook.Application.Services;
using FluentAssertions;
using Moq;
using NSubstitute;
using Xunit;

namespace Aktabook.Application.UnitTests.MessageHandlers;

public class ChangeBookInfoRequestStatusHandlerTest
{
private readonly Mock<IBookInfoRequester> _bookInfoRequestServiceMock = new(MockBehavior.Strict);
private readonly IBookInfoRequester _bookInfoRequestServiceMock = Substitute.For<IBookInfoRequester>();

[Fact]
public async Task GivenHandle_WhenCommand_ThenChangeBookInfoRequestStatus()
{
_bookInfoRequestServiceMock.Setup(x =>
x.ChangeRequestStatus(It.IsAny<Guid>(), It.IsAny<string>(),
It.IsAny<CancellationToken>())).ReturnsAsync(true);
_bookInfoRequestServiceMock.ChangeRequestStatus(Arg.Any<Guid>(), Arg.Any<string>(),
Arg.Any<CancellationToken>()).Returns(Task.FromResult(true));

ChangeBookInfoRequestStatus changeBookInfoRequestStatus =
new(new Guid("dddddddd-dddd-dddd-dddd-dddddddddddd"), "Dummy");
ChangeBookInfoRequestStatusHandler handler = new(_bookInfoRequestServiceMock.Object);
ChangeBookInfoRequestStatusHandler handler = new(_bookInfoRequestServiceMock);

bool result = await handler.Handle(changeBookInfoRequestStatus, CancellationToken.None)
.ConfigureAwait(false);

_bookInfoRequestServiceMock.Verify(
x => x.ChangeRequestStatus(
await _bookInfoRequestServiceMock.Received(1).ChangeRequestStatus(
new Guid("dddddddd-dddd-dddd-dddd-dddddddddddd"), "Dummy",
CancellationToken.None), Times.Once);
CancellationToken.None).ConfigureAwait(false);

result.Should().BeTrue();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,30 @@
using Aktabook.Application.Messages.Commands;
using Aktabook.Application.Services;
using FluentAssertions;
using Moq;
using NServiceBus;
using NSubstitute;
using Xunit;

namespace Aktabook.Application.UnitTests.MessageHandlers;

public class PlaceBookInfoRequestHandlerTest
{
private readonly Mock<IBookInfoRequester> _bookInfoRequestServiceMock = new(MockBehavior.Strict);
private readonly IBookInfoRequester _bookInfoRequestServiceMock = Substitute.For<IBookInfoRequester>();

private readonly Mock<IEndpointInstance> _endpointInstanceMock = new(MockBehavior.Strict);
private readonly IEndpointInstance _endpointInstanceMock = Substitute.For<IEndpointInstance>();

[Fact]
public async Task GivenHandle_WhenCommand_ThenBookInfoRequestId()
{
PlaceBookInfoRequest placeBookInfoRequest = new("Dummy ISBN");

_bookInfoRequestServiceMock.Setup(x =>
x.PlaceRequest(It.IsAny<string>(),
It.IsAny<CancellationToken>()))
.ReturnsAsync(new Guid("dddddddd-dddd-dddd-dddd-dddddddddddd"));
_bookInfoRequestServiceMock.PlaceRequest(Arg.Any<string>(), Arg.Any<CancellationToken>())
.Returns(Task.FromResult(new Guid("dddddddd-dddd-dddd-dddd-dddddddddddd")));

_endpointInstanceMock.Setup(x =>
x.Send(It.IsAny<object>(), It.IsAny<SendOptions>(), It.IsAny<CancellationToken>()))
_endpointInstanceMock.Send(Arg.Any<object>(), Arg.Any<SendOptions>(), Arg.Any<CancellationToken>())
.Returns(Task.CompletedTask);

PlaceBookInfoRequestHandler handler =
new(_bookInfoRequestServiceMock.Object,
_endpointInstanceMock.Object);
PlaceBookInfoRequestHandler handler = new(_bookInfoRequestServiceMock, _endpointInstanceMock);

Guid bookInfoRequestId = await handler.Handle(placeBookInfoRequest, CancellationToken.None)
.ConfigureAwait(false);
Expand Down
20 changes: 13 additions & 7 deletions test/Aktabook.Application.UnitTests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,21 @@
"Microsoft.TestPlatform.TestHost": "17.6.3"
}
},
"Moq": {
"NSubstitute": {
"type": "Direct",
"requested": "[4.18.4, )",
"resolved": "4.18.4",
"contentHash": "IOo+W51+7Afnb0noltJrKxPBSfsgMzTKCw+Re5AMx8l/vBbAbMDOynLik4+lBYIWDJSO0uV7Zdqt7cNb6RZZ+A==",
"requested": "[5.0.0, )",
"resolved": "5.0.0",
"contentHash": "CWGy4oXvcJcZzL7m5Rr/eMSejFXMDq1RRMlsbL6eltS3AuN/d8GH3ZzUcuStQcCSlcx+hpsgTxdnb3lhozWG6w==",
"dependencies": {
"Castle.Core": "5.1.1"
"Castle.Core": "5.0.0"
}
},
"NSubstitute.Analyzers.CSharp": {
"type": "Direct",
"requested": "[1.0.16, )",
"resolved": "1.0.16",
"contentHash": "+sdWbGyEQl22A4fBWo17/T53TMdbxegNjqwCxdBUS5/tp8ESJwfvHvGdIGj83sR0jKxvpUQoHO35qcf9tdgmdw=="
},
"System.Drawing.Common": {
"type": "Direct",
"requested": "[6.0.0, )",
Expand Down Expand Up @@ -119,8 +125,8 @@
},
"Castle.Core": {
"type": "Transitive",
"resolved": "5.1.1",
"contentHash": "rpYtIczkzGpf+EkZgDr9CClTdemhsrwA/W5hMoPjLkRFnXzH44zDLoovXeKtmxb1ykXK9aJVODSpiJml8CTw2g==",
"resolved": "5.0.0",
"contentHash": "edc8jjyXqzzy8jFdhs36FZdwmlDDTgqPb2Zy1Q5F/f2uAc88bu/VS/0Tpvgupmpl9zJOvOo5ZizVANb0ltN1NQ==",
"dependencies": {
"System.Diagnostics.EventLog": "6.0.0"
}
Expand Down
6 changes: 5 additions & 1 deletion test/Aktabook.Bus.UnitTest/Aktabook.Bus.UnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.3"/>
<PackageReference Include="Moq" Version="4.18.4"/>
<PackageReference Include="NServiceBus.Testing" Version="8.0.1"/>
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="NSubstitute.Analyzers.CSharp" Version="1.0.16">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="xunit" Version="2.5.0"/>
<PackageReference Include="xunit.analyzers" Version="1.2.0"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
Expand Down
Loading

0 comments on commit e2b9ae5

Please sign in to comment.