Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replacing Moq with NSubstitute #89

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" Version="1.1.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.5.0" />
<PackageReference Include="Microsoft.OpenApi" Version="1.6.3" />
<PackageReference Include="Moq" Version="4.18.4" />
<PackageReference Include="NSubstitute" Version="5.0.0" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down
3 changes: 1 addition & 2 deletions test/ApiFirstMediatR.Generator.Tests/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
global using ApiFirstMediatR.Generator.Models;
global using ApiFirstMediatR.Generator.Models.Config;
global using ApiFirstMediatR.Generator.Repositories;
global using ApiFirstMediatR.Generator.Services;
global using ApiFirstMediatR.Generator.Tests.Assertions;
global using ApiFirstMediatR.Generator.Tests.Utils;
global using FluentAssertions;
Expand All @@ -19,5 +18,5 @@
global using Microsoft.OpenApi;
global using Microsoft.OpenApi.Expressions;
global using Microsoft.OpenApi.Models;
global using Moq;
global using NSubstitute;
global using Xunit;
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ public class ControllerMapperTests
public ControllerMapperTests()
{
var mockApiConfigRepo = MockApiConfig.Create();
var mockOperationNamingRepository = new Mock<IOperationNamingRepository>();
var mockOperationNamingRepository = Substitute.For<IOperationNamingRepository>();
mockOperationNamingRepository
.Setup(mock => mock.GetControllerNameByPath("/test"))
.GetControllerNameByPath("/test")
.Returns("Test");

var mockDiagnosticReporter = new Mock<IDiagnosticReporter>();
var mockDiagnosticReporter = Substitute.For<IDiagnosticReporter>();
var typeMapper = new TypeMapper(mockApiConfigRepo);
var parameterMapper = new ParameterMapper(typeMapper);
var responseMapper = new ResponseMapper(typeMapper, mockOperationNamingRepository.Object);
var securityMapper = new SecurityMapper(mockDiagnosticReporter.Object);
var mockApiConfigRepository = new Mock<IApiConfigRepository>();
var responseMapper = new ResponseMapper(typeMapper, mockOperationNamingRepository);
var securityMapper = new SecurityMapper(mockDiagnosticReporter);
var mockApiConfigRepository = Substitute.For<IApiConfigRepository>();

var endpointMapper = new EndpointMapper(parameterMapper, responseMapper, typeMapper, securityMapper,
mockOperationNamingRepository.Object, mockApiConfigRepository.Object, mockDiagnosticReporter.Object);
mockOperationNamingRepository, mockApiConfigRepository, mockDiagnosticReporter);

_controllerMapper = new ControllerMapper(endpointMapper, mockOperationNamingRepository.Object);
_controllerMapper = new ControllerMapper(endpointMapper, mockOperationNamingRepository);
}

[Fact]
Expand Down
23 changes: 11 additions & 12 deletions test/ApiFirstMediatR.Generator.Tests/Mappers/EndpointMapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,33 @@ namespace ApiFirstMediatR.Generator.Tests.Mappers;
public class EndpointMapperTests
{
private readonly IEndpointMapper _endpointMapper;
private readonly Mock<IDiagnosticReporter> _mockDiagnosticReporter;
private readonly IDiagnosticReporter _mockDiagnosticReporter;

public EndpointMapperTests()
{
var mockApiConfigRepo = MockApiConfig.Create();
var mockOperationNamingRepository = new Mock<IOperationNamingRepository>();
var mockOperationNamingRepository = Substitute.For<IOperationNamingRepository>();
mockOperationNamingRepository
.Setup(mock => mock.GetControllerNameByOperationId("TestOperation"))
.GetControllerNameByOperationId("TestOperation")
.Returns("TestController");

mockOperationNamingRepository
.Setup(mock => mock.GetOperationNameByOperationId("TestOperation"))
.GetOperationNameByOperationId("TestOperation")
.Returns("TestOperation");

mockOperationNamingRepository
.Setup(mock => mock.GetOperationNameByPathAndOperationType("/test", OperationType.Get))
.GetOperationNameByPathAndOperationType("/test", OperationType.Get)
.Returns("TestOperation");

_mockDiagnosticReporter = new Mock<IDiagnosticReporter>();
_mockDiagnosticReporter = Substitute.For<IDiagnosticReporter>();
var typeMapper = new TypeMapper(mockApiConfigRepo);
var parameterMapper = new ParameterMapper(typeMapper);
var responseMapper = new ResponseMapper(typeMapper, mockOperationNamingRepository.Object);
var securityMapper = new SecurityMapper(_mockDiagnosticReporter.Object);
var mockApiConfigRepository = new Mock<IApiConfigRepository>();
var responseMapper = new ResponseMapper(typeMapper, mockOperationNamingRepository);
var securityMapper = new SecurityMapper(_mockDiagnosticReporter);
var mockApiConfigRepository = Substitute.For<IApiConfigRepository>();

_endpointMapper = new EndpointMapper(parameterMapper, responseMapper, typeMapper, securityMapper,
mockOperationNamingRepository.Object, mockApiConfigRepository.Object, _mockDiagnosticReporter.Object);
mockOperationNamingRepository, mockApiConfigRepository, _mockDiagnosticReporter);
}

[Fact]
Expand Down Expand Up @@ -195,8 +195,7 @@ public void InvalidRequestBody_ThrowsDiagnostic()

var endpoints = _endpointMapper.Map(paths);
endpoints.Should().BeEmpty();
_mockDiagnosticReporter.Verify(m => m.ReportDiagnostic(It.IsAny<Diagnostic>()));
_mockDiagnosticReporter.VerifyNoOtherCalls();
_mockDiagnosticReporter.Received(1).ReportDiagnostic(Arg.Any<Diagnostic>());
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ public class ResponseMapperTests
public ResponseMapperTests()
{
var mockApiConfigRepo = MockApiConfig.Create();
var mockOperationNamingRepository = new Mock<IOperationNamingRepository>();
var mockOperationNamingRepository = Substitute.For<IOperationNamingRepository>();
mockOperationNamingRepository
.Setup(mock => mock.GetControllerNameByOperationId("TestOperation"))
.GetControllerNameByOperationId("TestOperation")
.Returns("TestController");

mockOperationNamingRepository
.Setup(mock => mock.GetOperationNameByOperationId("TestOperation"))
.GetOperationNameByOperationId("TestOperation")
.Returns("TestOperation");

var typeMapper = new TypeMapper(mockApiConfigRepo);
var operationNamingRepository = mockOperationNamingRepository.Object;
var operationNamingRepository = mockOperationNamingRepository;

_responseMapper = new ResponseMapper(typeMapper, operationNamingRepository);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
public class SecurityMapperTests
{
private readonly ISecurityMapper _securityMapper;
private readonly Mock<IDiagnosticReporter> _mockDiagnosticReporter;
private readonly IDiagnosticReporter _mockDiagnosticReporter;

public SecurityMapperTests()
{
_mockDiagnosticReporter = new Mock<IDiagnosticReporter>();
_mockDiagnosticReporter = Substitute.For<IDiagnosticReporter>();

_securityMapper = new SecurityMapper(_mockDiagnosticReporter.Object);
_securityMapper = new SecurityMapper(_mockDiagnosticReporter);
}

[Fact]
Expand Down Expand Up @@ -75,6 +75,6 @@ public void Unsupported_ThrowsDiagnostic()

security.Policies.Should().NotBeNull();
security.Policies.Should().BeEmpty();
_mockDiagnosticReporter.Verify(m => m.ReportDiagnostic(It.IsAny<Diagnostic>()));
_mockDiagnosticReporter.Received(1).ReportDiagnostic(Arg.Any<Diagnostic>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@ namespace ApiFirstMediatR.Generator.Tests.Repositories;
public class ApiConfigRepositoryTests
{
private readonly ApiConfigRepository _apiConfigRepository;
private readonly Mock<AnalyzerConfigOptions> _mockAnalyzerConfigOptions;
private readonly Mock<ICompilation> _mockICompilation;
private readonly Mock<IDiagnosticReporter> _mockDiagnosticReporter;
private readonly AnalyzerConfigOptions _mockAnalyzerConfigOptions;
private readonly ICompilation _mockICompilation;
private readonly IDiagnosticReporter _mockDiagnosticReporter;

public ApiConfigRepositoryTests()
{
_mockAnalyzerConfigOptions = new Mock<AnalyzerConfigOptions>();
_mockAnalyzerConfigOptions = Substitute.For<AnalyzerConfigOptions>();

var mockAnalyzerConfigOptionsProvider = new Mock<AnalyzerConfigOptionsProvider>();
mockAnalyzerConfigOptionsProvider
.Setup(m => m.GlobalOptions)
.Returns(_mockAnalyzerConfigOptions.Object);
var analyzerConfigOptionsProvider = Substitute.For<AnalyzerConfigOptionsProvider>();
analyzerConfigOptionsProvider
.GlobalOptions
.Returns(_mockAnalyzerConfigOptions);

_mockICompilation = new Mock<ICompilation>();
_mockICompilation = Substitute.For<ICompilation>();
_mockICompilation
.Setup(m => m.AnalyzerConfigOptions)
.Returns(mockAnalyzerConfigOptionsProvider.Object);
.AnalyzerConfigOptions
.Returns(analyzerConfigOptionsProvider);

_mockDiagnosticReporter = new Mock<IDiagnosticReporter>();
_mockDiagnosticReporter = Substitute.For<IDiagnosticReporter>();

_apiConfigRepository = new ApiConfigRepository(_mockICompilation.Object, _mockDiagnosticReporter.Object);
_apiConfigRepository = new ApiConfigRepository(_mockICompilation, _mockDiagnosticReporter);
}

[Fact]
public void NoConfigValuesSet_HappyPath()
{
var compilation = CSharpCompilation.Create(null);
_mockICompilation
.Setup(m => m.Compilation)
.Compilation
.Returns(compilation);

var apiConfig = _apiConfigRepository.Get();
Expand All @@ -50,12 +50,16 @@ public void ConfigValuesSet_HappyPath()
{
var jsonLibrary = "Newtonsoft.Json";
_mockAnalyzerConfigOptions
.Setup(m => m.TryGetValue("build_property.ApiFirstMediatR_SerializationLibrary", out jsonLibrary))
.Returns(true);
.TryGetValue("build_property.ApiFirstMediatR_SerializationLibrary", out Arg.Any<string>()!)
.Returns(x =>
{
x[1] = jsonLibrary; // This is the out value
return true;
});

var compilation = CSharpCompilation.Create("ApiFirstChanged");
_mockICompilation
.Setup(m => m.Compilation)
.Compilation
.Returns(compilation);

var apiConfig = _apiConfigRepository.Get();
Expand All @@ -72,12 +76,16 @@ public void InvalidSerializationLibrary_ThrowsDiagnostic()
{
var jsonLibrary = "BadJsonSerializer";
_mockAnalyzerConfigOptions
.Setup(m => m.TryGetValue("build_property.ApiFirstMediatR_SerializationLibrary", out jsonLibrary))
.Returns(true);
.TryGetValue("build_property.ApiFirstMediatR_SerializationLibrary", out Arg.Any<string>()!)
.Returns(x =>
{
x[1] = jsonLibrary; // This is the out value
return true;
});

var compilation = CSharpCompilation.Create(null);
_mockICompilation
.Setup(m => m.Compilation)
.Compilation
.Returns(compilation);

var apiConfig = _apiConfigRepository.Get();
Expand All @@ -87,8 +95,7 @@ public void InvalidSerializationLibrary_ThrowsDiagnostic()

apiConfig.SerializationLibrary
.Should().Be(SerializationLibrary.SystemTextJson);

_mockDiagnosticReporter.Verify(m => m.ReportDiagnostic(It.IsAny<Diagnostic>()));
_mockDiagnosticReporter.VerifyNoOtherCalls();

_mockDiagnosticReporter.Received(1).ReportDiagnostic(Arg.Any<Diagnostic>());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ public class OperationNamingRepositoryTests
[Fact]
public void ValidAPISpec_OperationId_HappyPath()
{
var mockApiSpecRepository = new Mock<IApiSpecRepository>();
var mockApiSpecRepository = Substitute.For<IApiSpecRepository>();
mockApiSpecRepository
.Setup(mock => mock.Get())
.Get()
.Returns(new OpenApiDocument
{
Paths = new OpenApiPaths
Expand All @@ -33,7 +33,7 @@ public void ValidAPISpec_OperationId_HappyPath()
}
});

var operationNamingRepository = new OperationNamingRepository(mockApiSpecRepository.Object);
var operationNamingRepository = new OperationNamingRepository(mockApiSpecRepository);

const string expectedControllerName = "Api";
const string expectedOperationName = "GetHelloWorld";
Expand Down
6 changes: 3 additions & 3 deletions test/ApiFirstMediatR.Generator.Tests/Utils/MockApiConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ internal static class MockApiConfig
{
public static IApiConfigRepository Create(string namespaceName = "Test")
{
var mockApiConfigRepo = new Mock<IApiConfigRepository>();
var mockApiConfigRepo = Substitute.For<IApiConfigRepository>();
mockApiConfigRepo
.Setup(mock => mock.Get())
.Get()
.Returns(new ApiConfig
{
Namespace = namespaceName,
SerializationLibrary = SerializationLibrary.SystemTextJson,
RequestBodyName = "Body"
});

return mockApiConfigRepo.Object;
return mockApiConfigRepo;
}
}