diff --git a/test/ApiFirstMediatR.Generator.Tests/ApiFirstMediatR.Generator.Tests.csproj b/test/ApiFirstMediatR.Generator.Tests/ApiFirstMediatR.Generator.Tests.csproj index 789ecf5..a5092c7 100644 --- a/test/ApiFirstMediatR.Generator.Tests/ApiFirstMediatR.Generator.Tests.csproj +++ b/test/ApiFirstMediatR.Generator.Tests/ApiFirstMediatR.Generator.Tests.csproj @@ -16,7 +16,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/test/ApiFirstMediatR.Generator.Tests/GlobalUsings.cs b/test/ApiFirstMediatR.Generator.Tests/GlobalUsings.cs index b7432eb..0f81663 100644 --- a/test/ApiFirstMediatR.Generator.Tests/GlobalUsings.cs +++ b/test/ApiFirstMediatR.Generator.Tests/GlobalUsings.cs @@ -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; @@ -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; \ No newline at end of file diff --git a/test/ApiFirstMediatR.Generator.Tests/Mappers/ControllerMapperTests.cs b/test/ApiFirstMediatR.Generator.Tests/Mappers/ControllerMapperTests.cs index ef7cdc4..21cc4b8 100644 --- a/test/ApiFirstMediatR.Generator.Tests/Mappers/ControllerMapperTests.cs +++ b/test/ApiFirstMediatR.Generator.Tests/Mappers/ControllerMapperTests.cs @@ -7,22 +7,22 @@ public class ControllerMapperTests public ControllerMapperTests() { var mockApiConfigRepo = MockApiConfig.Create(); - var mockOperationNamingRepository = new Mock(); + var mockOperationNamingRepository = Substitute.For(); mockOperationNamingRepository - .Setup(mock => mock.GetControllerNameByPath("/test")) + .GetControllerNameByPath("/test") .Returns("Test"); - var mockDiagnosticReporter = new Mock(); + var mockDiagnosticReporter = Substitute.For(); 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(); + var responseMapper = new ResponseMapper(typeMapper, mockOperationNamingRepository); + var securityMapper = new SecurityMapper(mockDiagnosticReporter); + var mockApiConfigRepository = Substitute.For(); 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] diff --git a/test/ApiFirstMediatR.Generator.Tests/Mappers/EndpointMapperTests.cs b/test/ApiFirstMediatR.Generator.Tests/Mappers/EndpointMapperTests.cs index 421ff90..efdeece 100644 --- a/test/ApiFirstMediatR.Generator.Tests/Mappers/EndpointMapperTests.cs +++ b/test/ApiFirstMediatR.Generator.Tests/Mappers/EndpointMapperTests.cs @@ -3,33 +3,33 @@ namespace ApiFirstMediatR.Generator.Tests.Mappers; public class EndpointMapperTests { private readonly IEndpointMapper _endpointMapper; - private readonly Mock _mockDiagnosticReporter; + private readonly IDiagnosticReporter _mockDiagnosticReporter; public EndpointMapperTests() { var mockApiConfigRepo = MockApiConfig.Create(); - var mockOperationNamingRepository = new Mock(); + var mockOperationNamingRepository = Substitute.For(); 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(); + _mockDiagnosticReporter = Substitute.For(); 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(); + var responseMapper = new ResponseMapper(typeMapper, mockOperationNamingRepository); + var securityMapper = new SecurityMapper(_mockDiagnosticReporter); + var mockApiConfigRepository = Substitute.For(); _endpointMapper = new EndpointMapper(parameterMapper, responseMapper, typeMapper, securityMapper, - mockOperationNamingRepository.Object, mockApiConfigRepository.Object, _mockDiagnosticReporter.Object); + mockOperationNamingRepository, mockApiConfigRepository, _mockDiagnosticReporter); } [Fact] @@ -195,8 +195,7 @@ public void InvalidRequestBody_ThrowsDiagnostic() var endpoints = _endpointMapper.Map(paths); endpoints.Should().BeEmpty(); - _mockDiagnosticReporter.Verify(m => m.ReportDiagnostic(It.IsAny())); - _mockDiagnosticReporter.VerifyNoOtherCalls(); + _mockDiagnosticReporter.Received(1).ReportDiagnostic(Arg.Any()); } [Fact] diff --git a/test/ApiFirstMediatR.Generator.Tests/Mappers/ResponseMapperTests.cs b/test/ApiFirstMediatR.Generator.Tests/Mappers/ResponseMapperTests.cs index 891d80e..693f74f 100644 --- a/test/ApiFirstMediatR.Generator.Tests/Mappers/ResponseMapperTests.cs +++ b/test/ApiFirstMediatR.Generator.Tests/Mappers/ResponseMapperTests.cs @@ -7,17 +7,17 @@ public class ResponseMapperTests public ResponseMapperTests() { var mockApiConfigRepo = MockApiConfig.Create(); - var mockOperationNamingRepository = new Mock(); + var mockOperationNamingRepository = Substitute.For(); 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); } diff --git a/test/ApiFirstMediatR.Generator.Tests/Mappers/SecurityMapperTests.cs b/test/ApiFirstMediatR.Generator.Tests/Mappers/SecurityMapperTests.cs index f3d0351..0ee4b44 100644 --- a/test/ApiFirstMediatR.Generator.Tests/Mappers/SecurityMapperTests.cs +++ b/test/ApiFirstMediatR.Generator.Tests/Mappers/SecurityMapperTests.cs @@ -3,13 +3,13 @@ public class SecurityMapperTests { private readonly ISecurityMapper _securityMapper; - private readonly Mock _mockDiagnosticReporter; + private readonly IDiagnosticReporter _mockDiagnosticReporter; public SecurityMapperTests() { - _mockDiagnosticReporter = new Mock(); + _mockDiagnosticReporter = Substitute.For(); - _securityMapper = new SecurityMapper(_mockDiagnosticReporter.Object); + _securityMapper = new SecurityMapper(_mockDiagnosticReporter); } [Fact] @@ -75,6 +75,6 @@ public void Unsupported_ThrowsDiagnostic() security.Policies.Should().NotBeNull(); security.Policies.Should().BeEmpty(); - _mockDiagnosticReporter.Verify(m => m.ReportDiagnostic(It.IsAny())); + _mockDiagnosticReporter.Received(1).ReportDiagnostic(Arg.Any()); } } \ No newline at end of file diff --git a/test/ApiFirstMediatR.Generator.Tests/Repositories/ApiConfigRepositoryTests.cs b/test/ApiFirstMediatR.Generator.Tests/Repositories/ApiConfigRepositoryTests.cs index c2b14a4..916bd01 100644 --- a/test/ApiFirstMediatR.Generator.Tests/Repositories/ApiConfigRepositoryTests.cs +++ b/test/ApiFirstMediatR.Generator.Tests/Repositories/ApiConfigRepositoryTests.cs @@ -5,27 +5,27 @@ namespace ApiFirstMediatR.Generator.Tests.Repositories; public class ApiConfigRepositoryTests { private readonly ApiConfigRepository _apiConfigRepository; - private readonly Mock _mockAnalyzerConfigOptions; - private readonly Mock _mockICompilation; - private readonly Mock _mockDiagnosticReporter; + private readonly AnalyzerConfigOptions _mockAnalyzerConfigOptions; + private readonly ICompilation _mockICompilation; + private readonly IDiagnosticReporter _mockDiagnosticReporter; public ApiConfigRepositoryTests() { - _mockAnalyzerConfigOptions = new Mock(); + _mockAnalyzerConfigOptions = Substitute.For(); - var mockAnalyzerConfigOptionsProvider = new Mock(); - mockAnalyzerConfigOptionsProvider - .Setup(m => m.GlobalOptions) - .Returns(_mockAnalyzerConfigOptions.Object); + var analyzerConfigOptionsProvider = Substitute.For(); + analyzerConfigOptionsProvider + .GlobalOptions + .Returns(_mockAnalyzerConfigOptions); - _mockICompilation = new Mock(); + _mockICompilation = Substitute.For(); _mockICompilation - .Setup(m => m.AnalyzerConfigOptions) - .Returns(mockAnalyzerConfigOptionsProvider.Object); + .AnalyzerConfigOptions + .Returns(analyzerConfigOptionsProvider); - _mockDiagnosticReporter = new Mock(); + _mockDiagnosticReporter = Substitute.For(); - _apiConfigRepository = new ApiConfigRepository(_mockICompilation.Object, _mockDiagnosticReporter.Object); + _apiConfigRepository = new ApiConfigRepository(_mockICompilation, _mockDiagnosticReporter); } [Fact] @@ -33,7 +33,7 @@ public void NoConfigValuesSet_HappyPath() { var compilation = CSharpCompilation.Create(null); _mockICompilation - .Setup(m => m.Compilation) + .Compilation .Returns(compilation); var apiConfig = _apiConfigRepository.Get(); @@ -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()!) + .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(); @@ -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()!) + .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(); @@ -87,8 +95,7 @@ public void InvalidSerializationLibrary_ThrowsDiagnostic() apiConfig.SerializationLibrary .Should().Be(SerializationLibrary.SystemTextJson); - - _mockDiagnosticReporter.Verify(m => m.ReportDiagnostic(It.IsAny())); - _mockDiagnosticReporter.VerifyNoOtherCalls(); + + _mockDiagnosticReporter.Received(1).ReportDiagnostic(Arg.Any()); } } \ No newline at end of file diff --git a/test/ApiFirstMediatR.Generator.Tests/Repositories/OperationNamingRepositoryTests.cs b/test/ApiFirstMediatR.Generator.Tests/Repositories/OperationNamingRepositoryTests.cs index d985f80..286750f 100644 --- a/test/ApiFirstMediatR.Generator.Tests/Repositories/OperationNamingRepositoryTests.cs +++ b/test/ApiFirstMediatR.Generator.Tests/Repositories/OperationNamingRepositoryTests.cs @@ -5,9 +5,9 @@ public class OperationNamingRepositoryTests [Fact] public void ValidAPISpec_OperationId_HappyPath() { - var mockApiSpecRepository = new Mock(); + var mockApiSpecRepository = Substitute.For(); mockApiSpecRepository - .Setup(mock => mock.Get()) + .Get() .Returns(new OpenApiDocument { Paths = new OpenApiPaths @@ -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"; diff --git a/test/ApiFirstMediatR.Generator.Tests/Utils/MockApiConfig.cs b/test/ApiFirstMediatR.Generator.Tests/Utils/MockApiConfig.cs index d6a5e57..aab8de8 100644 --- a/test/ApiFirstMediatR.Generator.Tests/Utils/MockApiConfig.cs +++ b/test/ApiFirstMediatR.Generator.Tests/Utils/MockApiConfig.cs @@ -4,9 +4,9 @@ internal static class MockApiConfig { public static IApiConfigRepository Create(string namespaceName = "Test") { - var mockApiConfigRepo = new Mock(); + var mockApiConfigRepo = Substitute.For(); mockApiConfigRepo - .Setup(mock => mock.Get()) + .Get() .Returns(new ApiConfig { Namespace = namespaceName, @@ -14,6 +14,6 @@ public static IApiConfigRepository Create(string namespaceName = "Test") RequestBodyName = "Body" }); - return mockApiConfigRepo.Object; + return mockApiConfigRepo; } } \ No newline at end of file