From 4252c6492c332f4777d77e2c24847cd29c6089e1 Mon Sep 17 00:00:00 2001 From: wzh425 Date: Wed, 28 Aug 2024 11:55:05 +0800 Subject: [PATCH] test: Adjust unit testing --- .../ServiceCollectionExtensions.cs | 1 + .../IdentityTest.cs | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/ServiceCollectionExtensions.cs b/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/ServiceCollectionExtensions.cs index eff118481..a3cb07e86 100644 --- a/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/ServiceCollectionExtensions.cs +++ b/src/Contrib/Authentication/Identity/Masa.Contrib.Authentication.Identity.BlazorWebAssembly/ServiceCollectionExtensions.cs @@ -32,6 +32,7 @@ public static IServiceCollection AddMasaIdentity( private static IServiceCollection AddMasaIdentityCore(IServiceCollection services) { services.AddAuthorizationCore(); + services.TryAddScoped(); services.TryAddSingleton(); services.TryAddScoped(); return services; diff --git a/src/Contrib/Authentication/Identity/Tests/Masa.Contrib.Authentication.Identity.BlazorWebAssembly.Tests/IdentityTest.cs b/src/Contrib/Authentication/Identity/Tests/Masa.Contrib.Authentication.Identity.BlazorWebAssembly.Tests/IdentityTest.cs index 853678ae6..3cfc4f53b 100644 --- a/src/Contrib/Authentication/Identity/Tests/Masa.Contrib.Authentication.Identity.BlazorWebAssembly.Tests/IdentityTest.cs +++ b/src/Contrib/Authentication/Identity/Tests/Masa.Contrib.Authentication.Identity.BlazorWebAssembly.Tests/IdentityTest.cs @@ -21,7 +21,7 @@ public void TestMasaIdentity() } [TestMethod] - public void TestMasaIdentity2() + public async Task TestMasaIdentity2Async() { var services = new ServiceCollection(); var claimsPrincipal = new ClaimsPrincipal(new List() @@ -44,6 +44,9 @@ public void TestMasaIdentity2() option.UserId = "sub"; }); + var serviceProvider = services.BuildServiceProvider(); + await serviceProvider.InitializeApplicationAsync(); + Assert.IsTrue(services.Any(ServiceLifetime.Scoped)); Assert.IsTrue(services.Any(ServiceLifetime.Scoped)); Assert.IsTrue(services.Any(ServiceLifetime.Scoped)); @@ -51,7 +54,7 @@ public void TestMasaIdentity2() Assert.IsTrue(services.Any(ServiceLifetime.Scoped)); Assert.IsTrue(services.Any(ServiceLifetime.Scoped)); - var serviceProvider = services.BuildServiceProvider(); + var userContext = serviceProvider.GetService(); Assert.IsNotNull(userContext); Assert.AreEqual("1", userContext.UserId); @@ -63,7 +66,7 @@ public void TestMasaIdentity2() } [TestMethod] - public void TestIdentityByYaml() + public async Task TestIdentityByYamlAsync() { var services = new ServiceCollection(); services.AddMasaIdentity(string.Empty); @@ -95,6 +98,7 @@ public void TestIdentityByYaml() services.AddScoped(_ => authenticationStateProvider.Object); serviceProvider = services.BuildServiceProvider(); + await serviceProvider.InitializeApplicationAsync(); var userContext = serviceProvider.GetService(); Assert.IsNotNull(userContext); @@ -108,7 +112,7 @@ public void TestIdentityByYaml() } [TestMethod] - public void TestIdentityByYamlAndCustomOptions() + public async Task TestIdentityByYamlAndCustomOptionsAsync() { var services = new ServiceCollection(); services.AddMasaIdentity(string.Empty, option => @@ -143,6 +147,7 @@ public void TestIdentityByYamlAndCustomOptions() services.AddScoped(_ => authenticationStateProvider.Object); serviceProvider = services.BuildServiceProvider(); + await serviceProvider.InitializeApplicationAsync(); var userContext = serviceProvider.GetService(); Assert.IsNotNull(userContext);