Skip to content

Commit fd85b8d

Browse files
committed
Revert "fix: Change all bUnit services registration from singleton to scoped"
This reverts commit c34880d. # Conflicts: # CHANGELOG.md
1 parent 4353f88 commit fd85b8d

10 files changed

Lines changed: 45 additions & 60 deletions

File tree

CHANGELOG.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@ All notable changes to **bUnit** will be documented in this file. The project ad
1010

1111
- Update bunit templates to support the target framework version of the project. By [@linkdotnet](https://github.com/linkdotnet).
1212

13-
### Changed
14-
15-
- Change all bUnit services registration from singleton to scoped. By [@egil](https://github.com/egil). Closes https://github.com/bUnit-dev/bUnit/issues/1138.
16-
1713
## [1.21.9] - 2023-07-02
1814

1915
### Fixed

benchmark/bunit.benchmarks/BenchmarkBase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using BenchmarkDotNet.Attributes;
1+
using BenchmarkDotNet.Attributes;
22
using Bunit.Rendering;
33
using Microsoft.AspNetCore.Components;
44
using Microsoft.Extensions.DependencyInjection;
@@ -42,6 +42,6 @@ protected virtual void InternalCleanup()
4242

4343
protected virtual void RegisterServices(IServiceCollection serviceCollection)
4444
{
45-
services.AddScoped<BunitHtmlParser>();
45+
services.AddSingleton<BunitHtmlParser>();
4646
}
47-
}
47+
}

src/bunit.core/TestContextBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ protected TestContextBase()
5454
{
5555
Services = new TestServiceProvider();
5656
#if NET5_0_OR_GREATER
57-
Services.AddScoped<ComponentFactoryCollection>(_ => ComponentFactories);
57+
Services.AddSingleton<ComponentFactoryCollection>(_ => ComponentFactories);
5858
#endif
5959
}
6060

src/bunit.core/TestDoubles/PersistentComponentState/TestContextBaseExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ public static FakePersistentComponentState AddFakePersistentComponentState(this
2121
if (testContext is null)
2222
throw new ArgumentNullException(nameof(testContext));
2323

24-
testContext.Services.AddScoped<ComponentStatePersistenceManager>();
25-
testContext.Services.AddScoped<PersistentComponentState>(s => s.GetRequiredService<ComponentStatePersistenceManager>().State);
24+
testContext.Services.AddSingleton<ComponentStatePersistenceManager>();
25+
testContext.Services.AddSingleton<PersistentComponentState>(s => s.GetRequiredService<ComponentStatePersistenceManager>().State);
2626
return new FakePersistentComponentState(testContext.Services);
2727
}
2828
}

src/bunit.core/TestServiceProvider.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public object GetService(Type serviceType)
9393
{
9494
if (serviceProvider is null)
9595
{
96-
serviceCollection.AddScoped<TestServiceProvider>(_ => this);
96+
serviceCollection.AddSingleton<TestServiceProvider>(this);
9797
rootServiceProvider = serviceCollection.BuildServiceProvider(options);
9898
serviceScope = rootServiceProvider.CreateScope();
9999
serviceProvider = serviceScope.ServiceProvider;

src/bunit.web.testcomponents/Xunit.Sdk/RazorTestInvoker.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,6 @@ protected override object CallTestMethod(object testClassInstance)
4444

4545
private void RegisterXunitHelpersInTest(RazorTestBase test)
4646
{
47-
test.Services.AddScoped<ITestOutputHelper>(_ => testOutputHelperFactory());
47+
test.Services.AddSingleton<ITestOutputHelper>(_ => testOutputHelperFactory());
4848
}
4949
}

src/bunit.web/Extensions/TestServiceProviderExtensions.cs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,37 +28,37 @@ public static IServiceCollection AddDefaultTestContextServices(this IServiceColl
2828

2929
// Placeholders and defaults for common Blazor services
3030
services.AddLogging();
31-
services.AddScoped<AuthenticationStateProvider, PlaceholderAuthenticationStateProvider>();
32-
services.AddScoped<IAuthorizationService, PlaceholderAuthorizationService>();
33-
services.AddScoped<HttpClient, PlaceholderHttpClient>();
34-
services.AddScoped<IStringLocalizer, PlaceholderStringLocalization>();
31+
services.AddSingleton<AuthenticationStateProvider, PlaceholderAuthenticationStateProvider>();
32+
services.AddSingleton<IAuthorizationService, PlaceholderAuthorizationService>();
33+
services.AddSingleton<HttpClient, PlaceholderHttpClient>();
34+
services.AddSingleton<IStringLocalizer, PlaceholderStringLocalization>();
3535

3636
// bUnits fake JSInterop
37-
services.AddScoped<IJSRuntime>(_ => jsInterop.JSRuntime);
37+
services.AddSingleton<IJSRuntime>(jsInterop.JSRuntime);
3838

3939
// bUnits fake Navigation Manager
40-
services.AddScoped<FakeNavigationManager>();
41-
services.AddScoped<NavigationManager>(s => s.GetRequiredService<FakeNavigationManager>());
42-
services.AddScoped<INavigationInterception, FakeNavigationInterception>();
40+
services.AddSingleton<FakeNavigationManager>();
41+
services.AddSingleton<NavigationManager>(s => s.GetRequiredService<FakeNavigationManager>());
42+
services.AddSingleton<INavigationInterception, FakeNavigationInterception>();
4343

4444
// bUnits fake WebAssemblyHostEnvironment
45-
services.AddScoped<FakeWebAssemblyHostEnvironment>();
46-
services.AddScoped<IWebAssemblyHostEnvironment>(s => s.GetRequiredService<FakeWebAssemblyHostEnvironment>());
45+
services.AddSingleton<FakeWebAssemblyHostEnvironment>();
46+
services.AddSingleton<IWebAssemblyHostEnvironment>(s => s.GetRequiredService<FakeWebAssemblyHostEnvironment>());
4747

4848
// bUnit specific services
49-
services.AddScoped<TestContextBase>(_ => testContext);
50-
services.AddScoped<WebTestRenderer>();
51-
services.AddScoped<TestRenderer>(s => s.GetRequiredService<WebTestRenderer>());
52-
services.AddScoped<Renderer>(s => s.GetRequiredService<WebTestRenderer>());
53-
services.AddScoped<ITestRenderer>(s => s.GetRequiredService<WebTestRenderer>());
54-
services.AddScoped<HtmlComparer>();
55-
services.AddScoped<BunitHtmlParser>();
56-
services.AddScoped<IRenderedComponentActivator, RenderedComponentActivator>();
49+
services.AddSingleton<TestContextBase>(testContext);
50+
services.AddSingleton<WebTestRenderer>();
51+
services.AddSingleton<TestRenderer>(s => s.GetRequiredService<WebTestRenderer>());
52+
services.AddSingleton<Renderer>(s => s.GetRequiredService<WebTestRenderer>());
53+
services.AddSingleton<ITestRenderer>(s => s.GetRequiredService<WebTestRenderer>());
54+
services.AddSingleton<HtmlComparer>();
55+
services.AddSingleton<BunitHtmlParser>();
56+
services.AddSingleton<IRenderedComponentActivator, RenderedComponentActivator>();
5757

5858
services.AddMemoryCache();
5959

6060
#if NET6_0_OR_GREATER
61-
services.AddScoped<IErrorBoundaryLogger, BunitErrorBoundaryLogger>();
61+
services.AddSingleton<IErrorBoundaryLogger, BunitErrorBoundaryLogger>();
6262
#endif
6363
return services;
6464
}

src/bunit.web/TestDoubles/Authorization/FakeAuthorizationExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public static TestAuthorizationContext AddTestAuthorization(this TestContextBase
2020
throw new ArgumentNullException(nameof(context));
2121

2222
context.RenderTree.TryAdd<CascadingAuthenticationState>();
23-
context.Services.AddScoped<FakeSignOutSessionStateManager>();
23+
context.Services.AddSingleton<FakeSignOutSessionStateManager>();
2424
#pragma warning disable CS0618
25-
context.Services.AddScoped<SignOutSessionStateManager>(s => s.GetRequiredService<FakeSignOutSessionStateManager>());
25+
context.Services.AddSingleton<SignOutSessionStateManager>(s => s.GetRequiredService<FakeSignOutSessionStateManager>());
2626
#pragma warning restore CS0618
2727
var authCtx = new TestAuthorizationContext();
2828
authCtx.SetNotAuthorized();

src/bunit.web/TestDoubles/Authorization/TestAuthorizationContext.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,17 @@ public class TestAuthorizationContext
5656
/// <param name="services">Service provider to use.</param>
5757
public void RegisterAuthorizationServices(IServiceCollection services)
5858
{
59-
services.AddScoped<IAuthorizationService>(_ => authService);
60-
services.AddScoped<IAuthorizationPolicyProvider>(_ => policyProvider);
61-
services.AddScoped<AuthenticationStateProvider>(_ => authProvider);
59+
services.AddSingleton<IAuthorizationService>(authService);
60+
services.AddSingleton<IAuthorizationPolicyProvider>(policyProvider);
61+
services.AddSingleton<AuthenticationStateProvider>(authProvider);
6262
}
6363

6464
/// <summary>
6565
/// Authenticates the user with specified name and authorization state.
6666
/// </summary>
6767
/// <param name="userName">User name for the principal identity.</param>
6868
/// <param name="state">Authorization state.</param>
69-
public TestAuthorizationContext SetAuthorized(
70-
string userName,
71-
AuthorizationState state = AuthorizationState.Authorized)
69+
public TestAuthorizationContext SetAuthorized(string userName, AuthorizationState state = AuthorizationState.Authorized)
7270
{
7371
IsAuthenticated = true;
7472
UserName = userName;

tests/bunit.core.tests/Rendering/BunitComponentActivatorTest.cs

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,40 @@ namespace Bunit.Rendering;
66

77
public class BunitComponentActivatorTest : TestContext
88
{
9-
public static TheoryData<Action<IServiceCollection, IComponentActivator>> CustomActivatorRegistrations { get; } = new()
10-
{
11-
static (services, activator) => services.AddSingleton(activator),
12-
static (services, activator) => services.AddScoped(_ => activator),
13-
static (services, activator) => services.AddTransient(_ => activator),
14-
};
15-
16-
[Fact(DisplayName = "Default bUnit activator")]
9+
[Fact(DisplayName = "Default activator")]
1710
public void Test001()
1811
{
12+
var activator = new CustomComponentActivator();
13+
Services.AddSingleton<IComponentActivator>(activator);
14+
1915
var cut = RenderComponent<Simple1>();
2016

2117
cut.Instance.ShouldBeOfType<Simple1>();
2218
}
2319

24-
[Theory(DisplayName = "Custom IComponentActivator registered in Services")]
25-
[MemberData(nameof(CustomActivatorRegistrations))]
26-
public void Test002(Action<IServiceCollection, IComponentActivator> registerCustomActivator)
20+
[Fact(DisplayName = "Custom singleton IComponentActivator registered in Services")]
21+
public void Test002()
2722
{
2823
var activator = new CustomComponentActivator();
29-
registerCustomActivator(Services, activator);
24+
Services.AddSingleton<IComponentActivator>(activator);
3025

3126
RenderComponent<Simple1>();
3227

33-
activator
34-
.RequestedComponentTypes
28+
activator.RequestedComponentTypes
3529
.ShouldHaveSingleItem()
3630
.ShouldBe(typeof(Simple1));
3731
}
3832

39-
[Theory(DisplayName = "Custom singleton IComponentActivator registered in Services with ComponentFactories in use")]
40-
[MemberData(nameof(CustomActivatorRegistrations))]
41-
public void Test003(Action<IServiceCollection, IComponentActivator> registerCustomActivator)
33+
[Fact(DisplayName = "Custom singleton IComponentActivator registered in Services with ComponentFactories in use")]
34+
public void Test003()
4235
{
4336
var activator = new CustomComponentActivator();
44-
registerCustomActivator(Services, activator);
45-
37+
Services.AddSingleton<IComponentActivator>(activator);
4638
ComponentFactories.AddStub<ClickCounter>();
4739

4840
var cut = RenderComponent<Wrapper>(ps => ps.AddChildContent<ClickCounter>());
4941

50-
activator
51-
.RequestedComponentTypes
42+
activator.RequestedComponentTypes
5243
.ShouldHaveSingleItem()
5344
.ShouldBe(typeof(Wrapper));
5445
cut.HasComponent<ClickCounter>().ShouldBeFalse();

0 commit comments

Comments
 (0)