From e89b39f536e419b1c0b9bf380860cab05e5363a5 Mon Sep 17 00:00:00 2001 From: StuartFerguson Date: Wed, 2 Sep 2026 14:21:56 +0100 Subject: [PATCH] Replace Moq with Imposter in tests --- ...CallbackHandler.BusinessLogic.Tests.csproj | 2 +- .../ImposterGeneration.cs | 12 +++++++ .../Mediator/MediatorTests.cs | 14 ++++---- .../CallbackHandlerRequestHandlerTests.cs | 20 +++++------ .../Services/CallbackDomainServiceTests.cs | 36 +++++++++---------- CallbackHandler.Tests/BootstrapperTests.cs | 14 ++++---- .../CallbackHandler.Tests.csproj | 2 +- CallbackHandler.Tests/ImposterGeneration.cs | 4 +++ Directory.Packages.props | 4 +-- 9 files changed, 60 insertions(+), 48 deletions(-) create mode 100644 CallbackHandler.BusinessLogic.Tests/ImposterGeneration.cs create mode 100644 CallbackHandler.Tests/ImposterGeneration.cs diff --git a/CallbackHandler.BusinessLogic.Tests/CallbackHandler.BusinessLogic.Tests.csproj b/CallbackHandler.BusinessLogic.Tests/CallbackHandler.BusinessLogic.Tests.csproj index 5ef63f7..b4c17eb 100644 --- a/CallbackHandler.BusinessLogic.Tests/CallbackHandler.BusinessLogic.Tests.csproj +++ b/CallbackHandler.BusinessLogic.Tests/CallbackHandler.BusinessLogic.Tests.csproj @@ -10,7 +10,7 @@ - + diff --git a/CallbackHandler.BusinessLogic.Tests/ImposterGeneration.cs b/CallbackHandler.BusinessLogic.Tests/ImposterGeneration.cs new file mode 100644 index 0000000..a640378 --- /dev/null +++ b/CallbackHandler.BusinessLogic.Tests/ImposterGeneration.cs @@ -0,0 +1,12 @@ +using Imposter.Abstractions; +using Microsoft.AspNetCore.Hosting; +using SecurityService.Client; +using Shared.DomainDrivenDesign.EventSourcing; +using Shared.EventStore.Aggregate; +using TransactionProcessor.Client; + +[assembly: GenerateImposter(typeof(CallbackHandler.BusinessLogic.Services.ICallbackDomainService))] +[assembly: GenerateImposter(typeof(Shared.EventStore.Aggregate.IAggregateRepository<,>))] +[assembly: GenerateImposter(typeof(ISecurityServiceClient))] +[assembly: GenerateImposter(typeof(ITransactionProcessorClient))] +[assembly: GenerateImposter(typeof(IWebHostEnvironment))] diff --git a/CallbackHandler.BusinessLogic.Tests/Mediator/MediatorTests.cs b/CallbackHandler.BusinessLogic.Tests/Mediator/MediatorTests.cs index 2041ca2..a6a79f2 100644 --- a/CallbackHandler.BusinessLogic.Tests/Mediator/MediatorTests.cs +++ b/CallbackHandler.BusinessLogic.Tests/Mediator/MediatorTests.cs @@ -3,7 +3,7 @@ using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; -using Moq; +using Imposter.Abstractions; using System; using System.Collections.Generic; using System.Diagnostics; @@ -32,19 +32,19 @@ public MediatorTests() [Fact] public async Task Mediator_Send_RequestHandled() { - Mock hostingEnvironment = new(); - hostingEnvironment.Setup(he => he.EnvironmentName).Returns("Development"); - hostingEnvironment.Setup(he => he.ContentRootPath).Returns("/home"); - hostingEnvironment.Setup(he => he.ApplicationName).Returns("Test Application"); + IWebHostEnvironmentImposter hostingEnvironment = new(); + hostingEnvironment.EnvironmentName.Getter().Returns("Development"); + hostingEnvironment.ContentRootPath.Getter().Returns("/home"); + hostingEnvironment.ApplicationName.Getter().Returns("Test Application"); ServiceRegistry services = new(); - Startup s = new(hostingEnvironment.Object); + Startup s = new(hostingEnvironment.Instance()); IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddInMemoryCollection(TestData.DefaultAppSettings).Build(); //ConfigurationReader.Initialise(configurationRoot); Startup.Configuration = configurationRoot; - this.AddTestRegistrations(services, hostingEnvironment.Object); + this.AddTestRegistrations(services, hostingEnvironment.Instance()); s.ConfigureContainer(services); Startup.Container.AssertConfigurationIsValid(AssertMode.Full); diff --git a/CallbackHandler.BusinessLogic.Tests/RequestHandler/CallbackHandlerRequestHandlerTests.cs b/CallbackHandler.BusinessLogic.Tests/RequestHandler/CallbackHandlerRequestHandlerTests.cs index 2a6a4c9..8c1823a 100644 --- a/CallbackHandler.BusinessLogic.Tests/RequestHandler/CallbackHandlerRequestHandlerTests.cs +++ b/CallbackHandler.BusinessLogic.Tests/RequestHandler/CallbackHandlerRequestHandlerTests.cs @@ -11,7 +11,7 @@ namespace CallbackHandler.BusinessLogic.Tests.RequestHandler; using CallbackHandlers.Models; using CallbackMessageAggregate; using MediatR; -using Moq; +using Imposter.Abstractions; using Services; using Shared.DomainDrivenDesign.EventSourcing; using Shared.EventStore.Aggregate; @@ -23,12 +23,10 @@ public class CallbackHandlerRequestHandlerTests [Fact] public void CallbackHandlerRequestHandlerTests_RecordCallbackRequest_IsHandled() { - Mock domainService = new(); - domainService.Setup(a => a.RecordCallback(It.IsAny(), - It.IsAny())); - Mock> aggregateRepository = new(); + ICallbackDomainServiceImposter domainService = new(); + IAggregateRepositoryImposter aggregateRepository = new(); - CallbackHandlerRequestHandler handler = new(domainService.Object, aggregateRepository.Object); + CallbackHandlerRequestHandler handler = new(domainService.Instance(), aggregateRepository.Instance()); CallbackCommands.RecordCallbackCommand request = TestData.RecordCallbackCommand; @@ -38,17 +36,15 @@ public void CallbackHandlerRequestHandlerTests_RecordCallbackRequest_IsHandled() [Fact] public void CallbackHandlerRequestHandlerTests_GetCallbackQuery_IsHandled() { - Mock domainService = + ICallbackDomainServiceImposter domainService = new(); - //domainService.Setup(a => a.RecordCallback(It.IsAny(), - // It.IsAny())); - Mock> aggregateRepository = + IAggregateRepositoryImposter aggregateRepository = new(); - aggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny(), It.IsAny())) + aggregateRepository.GetLatestVersion(Arg.Any(), Arg.Any()) .ReturnsAsync(TestData.RecordedCallbackMessageAggregate()); - CallbackHandlerRequestHandler handler = new(domainService.Object, aggregateRepository.Object); + CallbackHandlerRequestHandler handler = new(domainService.Instance(), aggregateRepository.Instance()); CallbackQueries.GetCallbackQuery query = TestData.GetCallbackQuery; diff --git a/CallbackHandler.BusinessLogic.Tests/Services/CallbackDomainServiceTests.cs b/CallbackHandler.BusinessLogic.Tests/Services/CallbackDomainServiceTests.cs index 673ae23..5c951bc 100644 --- a/CallbackHandler.BusinessLogic.Tests/Services/CallbackDomainServiceTests.cs +++ b/CallbackHandler.BusinessLogic.Tests/Services/CallbackDomainServiceTests.cs @@ -8,6 +8,7 @@ using System.Threading.Tasks; using Shared.Logger; using TransactionProcessor.Client; +using Imposter.Abstractions; namespace CallbackHandler.BusinessLogic.Tests.Services; @@ -16,7 +17,6 @@ namespace CallbackHandler.BusinessLogic.Tests.Services; using CallbackHandlers.Models; using CallbackMessageAggregate; using Microsoft.Extensions.Configuration; -using Moq; using Shared.DomainDrivenDesign.EventSourcing; using Shared.EventStore.Aggregate; using Shouldly; @@ -27,21 +27,21 @@ public class CallbackDomainServiceTests { private readonly ICallbackDomainService DomainService; - private readonly Mock> AggregateRepository; - private readonly Mock SecurityServiceClient; - private readonly Mock TransactionProcessorClient; + private readonly IAggregateRepositoryImposter AggregateRepository; + private readonly ISecurityServiceClientImposter SecurityServiceClient; + private readonly ITransactionProcessorClientImposter TransactionProcessorClient; public CallbackDomainServiceTests() { - this.AggregateRepository = new Mock>(); - this.SecurityServiceClient = new Mock(); - this.TransactionProcessorClient = new Mock(); - this.DomainService = new CallbackDomainService(this.AggregateRepository.Object, this.SecurityServiceClient.Object, - this.TransactionProcessorClient.Object); - this.AggregateRepository.Setup(a => a.GetLatestVersion(It.IsAny(), It.IsAny())) + this.AggregateRepository = new(); + this.SecurityServiceClient = new(); + this.TransactionProcessorClient = new(); + this.DomainService = new CallbackDomainService(this.AggregateRepository.Instance(), this.SecurityServiceClient.Instance(), + this.TransactionProcessorClient.Instance()); + this.AggregateRepository.GetLatestVersion(Arg.Any(), Arg.Any()) .ReturnsAsync(TestData.EmptyCallbackMessageAggregate()); - this.AggregateRepository.Setup(a => a.SaveChanges(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Success); - this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.TokenResponse())); - this.TransactionProcessorClient.Setup(t => t.GetMerchant(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success()); + this.AggregateRepository.SaveChanges(Arg.Any(), Arg.Any()) + .ReturnsAsync(Result.Success()); + this.SecurityServiceClient.GetToken(Arg.Any(), Arg.Any(), Arg.Any()).ReturnsAsync(Result.Success(TestData.TokenResponse())); + this.TransactionProcessorClient.GetMerchant(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()).ReturnsAsync(Result.Success()); IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddInMemoryCollection(TestData.DefaultAppSettings).Build(); ConfigurationReader.Initialise(configurationRoot); @@ -85,7 +85,7 @@ public async Task CallbackDomainService_RecordCallback_MerchantIdNotValidGuid_Re [Fact] public async Task CallbackDomainService_RecordCallback_GetTokenFailed_ResultFailed() { - this.SecurityServiceClient.Setup(s => s.GetToken(It.IsAny(), It.IsAny(), It.IsAny())) + this.SecurityServiceClient.GetToken(Arg.Any(), Arg.Any(), Arg.Any()) .ReturnsAsync(Result.Failure()); Result result = await this.DomainService.RecordCallback(TestData.RecordCallbackCommand, CancellationToken.None); result.IsFailed.ShouldBeTrue(); @@ -94,7 +94,7 @@ public async Task CallbackDomainService_RecordCallback_GetTokenFailed_ResultFail [Fact] public async Task CallbackDomainService_RecordCallback_GetMerchantFailed_ResultFailed() { - this.TransactionProcessorClient.Setup(t => t.GetMerchant(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(Result.Failure()); + this.TransactionProcessorClient.GetMerchant(Arg.Any(), Arg.Any(), Arg.Any(), Arg.Any()).ReturnsAsync(Result.Failure()); Result result = await this.DomainService.RecordCallback(TestData.RecordCallbackCommand, CancellationToken.None); result.IsFailed.ShouldBeTrue(); @@ -103,8 +103,8 @@ public async Task CallbackDomainService_RecordCallback_GetMerchantFailed_ResultF [Fact] public async Task CallbackDomainService_RecordCallback_SaveFailed_ResultFailed() { - this.AggregateRepository.Setup(a => a.SaveChanges(It.IsAny(), It.IsAny())) - .ReturnsAsync(Result.Failure); + this.AggregateRepository.SaveChanges(Arg.Any(), Arg.Any()) + .ReturnsAsync(Result.Failure()); Result result = await this.DomainService.RecordCallback(TestData.RecordCallbackCommand, CancellationToken.None); result.IsFailed.ShouldBeTrue(); } diff --git a/CallbackHandler.Tests/BootstrapperTests.cs b/CallbackHandler.Tests/BootstrapperTests.cs index abbacb1..ff29157 100644 --- a/CallbackHandler.Tests/BootstrapperTests.cs +++ b/CallbackHandler.Tests/BootstrapperTests.cs @@ -6,11 +6,11 @@ namespace CallbackHandler.Tests using System.Collections.Generic; using System.Diagnostics; using Lamar; + using Imposter.Abstractions; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; - using Moq; [Collection("TestCollection")] public class BootstrapperTests @@ -18,16 +18,16 @@ public class BootstrapperTests [Fact] public void VerifyBootstrapperIsValid() { - Mock hostingEnvironment = new(); - hostingEnvironment.Setup(he => he.EnvironmentName).Returns("Development"); - hostingEnvironment.Setup(he => he.ContentRootPath).Returns("/home"); - hostingEnvironment.Setup(he => he.ApplicationName).Returns("Test Application"); + IWebHostEnvironmentImposter hostingEnvironment = new(); + hostingEnvironment.EnvironmentName.Getter().Returns("Development"); + hostingEnvironment.ContentRootPath.Getter().Returns("/home"); + hostingEnvironment.ApplicationName.Getter().Returns("Test Application"); ServiceRegistry services = new(); - Startup s = new(hostingEnvironment.Object); + Startup s = new(hostingEnvironment.Instance()); Startup.Configuration = this.SetupMemoryConfiguration(); - this.AddTestRegistrations(services, hostingEnvironment.Object); + this.AddTestRegistrations(services, hostingEnvironment.Instance()); s.ConfigureContainer(services); Startup.Container.AssertConfigurationIsValid(AssertMode.Full); } diff --git a/CallbackHandler.Tests/CallbackHandler.Tests.csproj b/CallbackHandler.Tests/CallbackHandler.Tests.csproj index 8f5aabe..8e53062 100644 --- a/CallbackHandler.Tests/CallbackHandler.Tests.csproj +++ b/CallbackHandler.Tests/CallbackHandler.Tests.csproj @@ -13,7 +13,7 @@ - + runtime; build; native; contentfiles; analyzers; buildtransitive diff --git a/CallbackHandler.Tests/ImposterGeneration.cs b/CallbackHandler.Tests/ImposterGeneration.cs new file mode 100644 index 0000000..fc76b0d --- /dev/null +++ b/CallbackHandler.Tests/ImposterGeneration.cs @@ -0,0 +1,4 @@ +using Imposter.Abstractions; +using Microsoft.AspNetCore.Hosting; + +[assembly: GenerateImposter(typeof(IWebHostEnvironment))] diff --git a/Directory.Packages.props b/Directory.Packages.props index 12847c8..6d1bfc6 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -8,7 +8,7 @@ - + @@ -44,4 +44,4 @@ - \ No newline at end of file +