From 33a26984c8adfbdb3485c149f7e0a91af2ff50c0 Mon Sep 17 00:00:00 2001 From: Thomas Rittson Date: Mon, 26 May 2025 16:37:21 +1000 Subject: [PATCH] Combine static methods --- test/Core.Test/Services/UserServiceTests.cs | 42 +++++++++++++-------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/test/Core.Test/Services/UserServiceTests.cs b/test/Core.Test/Services/UserServiceTests.cs index 7845fa9f11..829ac2ce80 100644 --- a/test/Core.Test/Services/UserServiceTests.cs +++ b/test/Core.Test/Services/UserServiceTests.cs @@ -170,8 +170,7 @@ public class UserServiceTests User user) { var sutProvider = new SutProvider() - .WithFakeTokenProvider(user) - .Create(); + .CreateWithUserServiceCustomizations(user); var context = sutProvider.GetDependency(); context.DeviceType = deviceType; @@ -188,8 +187,7 @@ public class UserServiceTests public async Task SendNewDeviceVerificationEmailAsync_NullDeviceTypeShouldSendUnkownBrowserType(User user) { var sutProvider = new SutProvider() - .WithFakeTokenProvider(user) - .Create(); + .CreateWithUserServiceCustomizations(user); var context = sutProvider.GetDependency(); context.DeviceType = null; @@ -266,10 +264,7 @@ public class UserServiceTests SetupUserAndDevice(user, shouldHavePassword); var sutProvider = new SutProvider() - .WithUserPasswordStore() - .WithFakeTokenProvider(user) - .Create() - .FixPasswordHasherBug(); + .CreateWithUserServiceCustomizations(user); // Setup the fake password verification sutProvider.GetDependency>() @@ -498,10 +493,7 @@ public class UserServiceTests SetupUserAndDevice(user, true); var sutProvider = new SutProvider() - .WithUserPasswordStore() - .WithFakeTokenProvider(user) - .Create() - .FixPasswordHasherBug(); + .CreateWithUserServiceCustomizations(user); // Setup the fake password verification sutProvider @@ -679,7 +671,7 @@ public static class UserServiceSutProviderExtensions /// Arranges a fake token provider. Must call as part of a builder pattern that ends in Create(), as it modifies /// the SutProvider build chain. /// - public static SutProvider WithFakeTokenProvider(this SutProvider sutProvider, User user) + private static SutProvider SetFakeTokenProvider(this SutProvider sutProvider, User user) { var fakeUserTwoFactorProvider = Substitute.For>(); @@ -720,14 +712,20 @@ public static class UserServiceSutProviderExtensions return sutProvider; } - public static SutProvider WithUserPasswordStore(this SutProvider sutProvider) + /// + /// Properly registers IUserPasswordStore as IUserStore so it's injected when the sut is initialized. + /// + /// + /// + private static SutProvider SetUserPasswordStore(this SutProvider sutProvider) { var substitutedUserPasswordStore = Substitute.For>(); // IUserPasswordStore must be registered under the IUserStore parameter to be properly injected + // because this is what the constructor expects sutProvider.SetDependency>(substitutedUserPasswordStore); - // also store it under its own type for retrieval and configuration + // Also store it under its own type for retrieval and configuration sutProvider.SetDependency(substitutedUserPasswordStore); return sutProvider; @@ -739,10 +737,22 @@ public static class UserServiceSutProviderExtensions /// This doesn't usually happen because our dependencies are not usually public. /// Call this AFTER SutProvider.Create(). /// - public static SutProvider FixPasswordHasherBug(this SutProvider sutProvider) + private static SutProvider FixPasswordHasherBug(this SutProvider sutProvider) { // Get the configured sutProvider mock and assign it back to the public property in the base class sutProvider.Sut.PasswordHasher = sutProvider.GetDependency>(); return sutProvider; } + + /// + /// A helper that combines all SutProvider configuration usually required for UserService. + /// Call this instead of SutProvider.Create, after any additional configuration your test needs. + /// + public static SutProvider CreateWithUserServiceCustomizations(this SutProvider sutProvider, User user) + => sutProvider + .SetUserPasswordStore() + .SetFakeTokenProvider(user) + .Create() + .FixPasswordHasherBug(); + }