From b3006ef64e2e3b5ba2f2f4ae03a95b80eeebaaba Mon Sep 17 00:00:00 2001 From: Thomas Rittson Date: Mon, 7 Oct 2024 08:44:53 +1000 Subject: [PATCH] Add test helper methods --- .../Services/PolicyServicevNextTests.cs | 75 +++++++------------ 1 file changed, 28 insertions(+), 47 deletions(-) diff --git a/test/Core.Test/AdminConsole/Services/PolicyServicevNextTests.cs b/test/Core.Test/AdminConsole/Services/PolicyServicevNextTests.cs index 8faa4e34ef..4ad8828adf 100644 --- a/test/Core.Test/AdminConsole/Services/PolicyServicevNextTests.cs +++ b/test/Core.Test/AdminConsole/Services/PolicyServicevNextTests.cs @@ -46,14 +46,7 @@ public class PolicyServicevNextTests Guid.NewGuid())); Assert.Contains("Organization not found", badRequestException.Message, StringComparison.OrdinalIgnoreCase); - - await sutProvider.GetDependency() - .DidNotReceiveWithAnyArgs() - .UpsertAsync(default); - - await sutProvider.GetDependency() - .DidNotReceiveWithAnyArgs() - .LogPolicyEventAsync(default, default, default); + await AssertPolicyNotSavedAsync(sutProvider); } [Theory, BitAutoData] @@ -75,27 +68,14 @@ public class PolicyServicevNextTests Guid.NewGuid())); Assert.Contains("cannot use policies", badRequestException.Message, StringComparison.OrdinalIgnoreCase); - - await sutProvider.GetDependency() - .DidNotReceiveWithAnyArgs() - .UpsertAsync(default); - - await sutProvider.GetDependency() - .DidNotReceiveWithAnyArgs() - .LogPolicyEventAsync(default, default, default); + await AssertPolicyNotSavedAsync(sutProvider); } [Theory, BitAutoData] public async Task SaveAsync_PolicyDefinitionNotFound_Throws([Policy(PolicyType.SingleOrg)]Policy policy) { var sutProvider = SutProviderFactory(); - sutProvider.GetDependency() - .GetOrganizationAbilityAsync(policy.OrganizationId) - .Returns(new OrganizationAbility - { - Id = policy.OrganizationId, - UsePolicies = true - }); + ArrangeOrganization(sutProvider, policy); var exception = await Assert.ThrowsAsync( () => sutProvider.Sut.SaveAsync(policy, @@ -104,14 +84,7 @@ public class PolicyServicevNextTests Guid.NewGuid())); Assert.Contains("No PolicyDefinition found for SingleOrg policy", exception.Message, StringComparison.OrdinalIgnoreCase); - - await sutProvider.GetDependency() - .DidNotReceiveWithAnyArgs() - .UpsertAsync(default); - - await sutProvider.GetDependency() - .DidNotReceiveWithAnyArgs() - .LogPolicyEventAsync(default, default, default); + await AssertPolicyNotSavedAsync(sutProvider); } [Theory, BitAutoData] @@ -121,14 +94,7 @@ public class PolicyServicevNextTests fakePolicyDefinition.ValidateAsyncMock(null, policy).Returns("Validation error!"); var sutProvider = SutProviderFactory([fakePolicyDefinition]); - sutProvider.GetDependency() - .GetOrganizationAbilityAsync(policy.OrganizationId) - .Returns(new OrganizationAbility - { - Id = policy.OrganizationId, - UsePolicies = true - }); - + ArrangeOrganization(sutProvider, policy); sutProvider.GetDependency().GetManyByOrganizationIdAsync(policy.OrganizationId).Returns([]); var badRequestException = await Assert.ThrowsAsync( @@ -138,14 +104,7 @@ public class PolicyServicevNextTests Guid.NewGuid())); Assert.Contains("Validation error!", badRequestException.Message, StringComparison.OrdinalIgnoreCase); - - await sutProvider.GetDependency() - .DidNotReceiveWithAnyArgs() - .UpsertAsync(default); - - await sutProvider.GetDependency() - .DidNotReceiveWithAnyArgs() - .LogPolicyEventAsync(default, default, default); + await AssertPolicyNotSavedAsync(sutProvider); } /// @@ -159,4 +118,26 @@ public class PolicyServicevNextTests sutProvider.Create(); return sutProvider; } + + private static void ArrangeOrganization(SutProvider sutProvider, Policy policy) + { + sutProvider.GetDependency() + .GetOrganizationAbilityAsync(policy.OrganizationId) + .Returns(new OrganizationAbility + { + Id = policy.OrganizationId, + UsePolicies = true + }); + } + + private static async Task AssertPolicyNotSavedAsync(SutProvider sutProvider) + { + await sutProvider.GetDependency() + .DidNotReceiveWithAnyArgs() + .UpsertAsync(default); + + await sutProvider.GetDependency() + .DidNotReceiveWithAnyArgs() + .LogPolicyEventAsync(default, default, default); + } }