Skip to content

Commit 9599046

Browse files
committed
refactor: Rename TestServiceProvider to BunitServiceProvider (#1416)
1 parent 08e619a commit 9599046

File tree

13 files changed

+47
-44
lines changed

13 files changed

+47
-44
lines changed

MIGRATION.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,6 @@ public class HelloWorldInstancePerTestCase : Bunit.TestContext
7676
```
7777

7878
Or use the `BunitContext` directly and manage the lifecycle yourself.
79+
80+
## `TestServiceProvider` renamed to `BunitTestServiceProvider`
81+
The `TestServiceProvider` class has been renamed to `BunitTestServiceProvider`. If you used `TestServiceProvider`, you should replace it with `BunitTestServiceProvider`.

docs/samples/tests/xunit/MockHttpClientBunitHelpers.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Bunit.Docs.Samples;
1111

1212
public static class MockHttpClientBunitHelpers
1313
{
14-
public static MockHttpMessageHandler AddMockHttpClient(this TestServiceProvider services)
14+
public static MockHttpMessageHandler AddMockHttpClient(this BunitServiceProvider services)
1515
{
1616
var mockHttpHandler = new MockHttpMessageHandler();
1717
var httpClient = mockHttpHandler.ToHttpClient();

docs/site/docs/providing-input/inject-services-into-components.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ The highlighted line shows how the `IWeatherForecastService` is registered in th
2828

2929
## Fallback service provider
3030

31-
A fallback service provider can be registered with the built-in `TestServiceProvider`. This enables a few interesting use cases, such as using an alternative IoC container (which should implement the `IServiceProvider` interface), or automatically creating mock services for your Blazor components. The latter can be achieved by using a combination of [AutoFixture](https://github.com/AutoFixture/AutoFixture) and your favorite mocking framework, e.g. Moq, NSubsitute, or Telerik JustMock.
31+
A fallback service provider can be registered with the built-in `BunitServiceProvider`. This enables a few interesting use cases, such as using an alternative IoC container (which should implement the `IServiceProvider` interface), or automatically creating mock services for your Blazor components. The latter can be achieved by using a combination of [AutoFixture](https://github.com/AutoFixture/AutoFixture) and your favorite mocking framework, e.g. Moq, NSubsitute, or Telerik JustMock.
3232

3333
### When is the fallback service provider used?
3434

35-
The logic inside the `TestServiceProvider` for using the fallback service provider is as follows:
35+
The logic inside the `BunitServiceProvider` for using the fallback service provider is as follows:
3636

3737
1. Try resolving the requested service from the standard service provider in bUnit.
3838
2. If that fails, try resolving from a fallback service provider, if one exists.
@@ -52,7 +52,7 @@ Here is a test where the fallback service provider is used:
5252
In this example, the `DummyService` is provided by the fallback service provider, since it is not registered in the default service provider.
5353

5454
## Using a custom IServiceProvider implementation
55-
A custom service provider factory can be registered with the built-in `TestServiceProvider`. It is used to create the underlying IServiceProvider. This enables a few interesting use cases, such as using an alternative IoC container (which should implement the `IServiceProvider` interface). This approach can be useful if the fallback service provider is not an option. For example, if you have dependencies in the fallback container, that rely on dependencies which are in the main container and vice versa.
55+
A custom service provider factory can be registered with the built-in `BunitServiceProvider`. It is used to create the underlying IServiceProvider. This enables a few interesting use cases, such as using an alternative IoC container (which should implement the `IServiceProvider` interface). This approach can be useful if the fallback service provider is not an option. For example, if you have dependencies in the fallback container, that rely on dependencies which are in the main container and vice versa.
5656

5757
### Registering Autofac service provider factory
5858
The example makes use of `AutofacServiceProviderFactory` and `AutofacServiceProvider` from the package `Autofac.Extensions.DependencyInjection` and shows how to use an Autofac dependency container with bUnit.

src/bunit/Asserting/MarkupMatchesAssertExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ public static void MarkupMatches(this RenderedFragment actual, RenderFragment ex
275275
// TODO: This will be obsolete with: https://github.com/bUnit-dev/bUnit/issues/1018
276276
// As the renderer would be transient we don't have to new up an instance
277277
using var renderer = new BunitRenderer(
278-
actual.Services.GetRequiredService<TestServiceProvider>(),
278+
actual.Services.GetRequiredService<BunitServiceProvider>(),
279279
actual.Services.GetRequiredService<ILoggerFactory>());
280280
var renderedFragment = renderer.RenderFragment(expected);
281281
MarkupMatches(actual, renderedFragment, userMessage);

src/bunit/BunitContext.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public partial class BunitContext : IDisposable, IAsyncDisposable
3333
/// Gets the service collection and service provider that is used when a
3434
/// component is rendered by the test context.
3535
/// </summary>
36-
public TestServiceProvider Services { get; }
36+
public BunitServiceProvider Services { get; }
3737

3838
/// <summary>
3939
/// Gets the <see cref="RootRenderTree"/> that all components rendered with the
@@ -58,7 +58,7 @@ public partial class BunitContext : IDisposable, IAsyncDisposable
5858
/// </summary>
5959
public BunitContext()
6060
{
61-
Services = new TestServiceProvider();
61+
Services = new BunitServiceProvider();
6262
Services.AddSingleton<ComponentFactoryCollection>(_ => ComponentFactories);
6363
Services.AddDefaultBunitContextServices(this, JSInterop);
6464
}

src/bunit/TestServiceProvider.cs renamed to src/bunit/BunitServiceProvider.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Bunit;
66
/// Represents a <see cref="IServiceProvider"/> and <see cref="IServiceCollection"/>
77
/// as a single type used for test purposes.
88
/// </summary>
9-
public sealed class TestServiceProvider : IKeyedServiceProvider, IServiceCollection, IDisposable, IAsyncDisposable
9+
public sealed class BunitServiceProvider : IKeyedServiceProvider, IServiceCollection, IDisposable, IAsyncDisposable
1010
{
1111
private static readonly ServiceProviderOptions DefaultServiceProviderOptions = new() { ValidateScopes = true };
1212
private readonly IServiceCollection serviceCollection;
@@ -18,7 +18,7 @@ public sealed class TestServiceProvider : IKeyedServiceProvider, IServiceCollect
1818
private Func<IServiceProvider> serviceProviderFactory;
1919

2020
/// <summary>
21-
/// Gets a value indicating whether this <see cref="TestServiceProvider"/> has been initialized, and
21+
/// Gets a value indicating whether this <see cref="BunitServiceProvider"/> has been initialized, and
2222
/// no longer will accept calls to the <c>AddService</c>'s methods.
2323
/// </summary>
2424
public bool IsProviderInitialized => serviceProvider is not null;
@@ -50,15 +50,15 @@ public ServiceProviderOptions Options
5050
}
5151

5252
/// <summary>
53-
/// Initializes a new instance of the <see cref="TestServiceProvider"/> class
53+
/// Initializes a new instance of the <see cref="BunitServiceProvider"/> class
5454
/// and sets its service collection to the provided <paramref name="initialServiceCollection"/>, if any.
5555
/// </summary>
56-
public TestServiceProvider(IServiceCollection? initialServiceCollection = null)
56+
public BunitServiceProvider(IServiceCollection? initialServiceCollection = null)
5757
: this(initialServiceCollection ?? new ServiceCollection(), initializeProvider: false)
5858
{
5959
}
6060

61-
private TestServiceProvider(IServiceCollection initialServiceCollection, bool initializeProvider)
61+
private BunitServiceProvider(IServiceCollection initialServiceCollection, bool initializeProvider)
6262
{
6363
serviceCollection = initialServiceCollection;
6464
serviceProviderFactory = () => serviceCollection.BuildServiceProvider(Options);
@@ -110,7 +110,7 @@ private void InitializeProvider()
110110
{
111111
CheckInitializedAndThrow();
112112

113-
serviceCollection.AddSingleton<TestServiceProvider>(this);
113+
serviceCollection.AddSingleton<BunitServiceProvider>(this);
114114
rootServiceProvider = serviceProviderFactory.Invoke();
115115
serviceScope = rootServiceProvider.CreateScope();
116116
serviceProvider = serviceScope.ServiceProvider;

src/bunit/Extensions/TestServiceProviderExtensions.cs renamed to src/bunit/Extensions/BunitServiceProviderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Bunit.Extensions;
1212
/// <summary>
1313
/// Helper methods for correctly registering test dependencies.
1414
/// </summary>
15-
public static class TestServiceProviderExtensions
15+
public static class BunitServiceProviderExtensions
1616
{
1717
/// <summary>
1818
/// Registers the default services required by the web <see cref="BunitContext"/>.

src/bunit/Rendering/BunitRenderer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Bunit.Rendering;
1010
/// </summary>
1111
public sealed class BunitRenderer : Renderer
1212
{
13-
private readonly TestServiceProvider services;
13+
private readonly BunitServiceProvider services;
1414

1515
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_isBatchInProgress")]
1616
private static extern ref bool GetIsBatchInProgressField(Renderer renderer);
@@ -57,7 +57,7 @@ private bool IsBatchInProgress
5757
/// <summary>
5858
/// Initializes a new instance of the <see cref="BunitRenderer"/> class.
5959
/// </summary>
60-
public BunitRenderer(TestServiceProvider services, ILoggerFactory loggerFactory)
60+
public BunitRenderer(BunitServiceProvider services, ILoggerFactory loggerFactory)
6161
: base(services, loggerFactory, new BunitComponentActivator(services.GetRequiredService<ComponentFactoryCollection>(), null))
6262
{
6363
this.services = services;
@@ -68,7 +68,7 @@ public BunitRenderer(TestServiceProvider services, ILoggerFactory loggerFactory)
6868
/// <summary>
6969
/// Initializes a new instance of the <see cref="BunitRenderer"/> class.
7070
/// </summary>
71-
public BunitRenderer(TestServiceProvider services, ILoggerFactory loggerFactory, IComponentActivator componentActivator)
71+
public BunitRenderer(BunitServiceProvider services, ILoggerFactory loggerFactory, IComponentActivator componentActivator)
7272
: base(services, loggerFactory, new BunitComponentActivator(services.GetRequiredService<ComponentFactoryCollection>(), componentActivator))
7373
{
7474
this.services = services;

src/bunit/Rendering/RenderedFragment.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Bunit;
1010
[DebuggerDisplay("Rendered:{RenderCount}")]
1111
public class RenderedFragment : IDisposable
1212
{
13-
[SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "Owned by TestServiceProvider, disposed by it.")]
13+
[SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "Owned by BunitServiceProvider, disposed by it.")]
1414
private readonly BunitHtmlParser htmlParser;
1515
private string markup = string.Empty;
1616
private INodeList? latestRenderNodes;

src/bunit/TestDoubles/Authorization/BunitAuthorizationContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class BunitAuthorizationContext
5353
/// <summary>
5454
/// Creates a new instance of <see cref="BunitAuthorizationContext"/>.
5555
/// </summary>
56-
internal BunitAuthorizationContext(TestServiceProvider services)
56+
internal BunitAuthorizationContext(BunitServiceProvider services)
5757
{
5858
services.AddSingleton<IAuthorizationService>(authService);
5959
services.AddSingleton<IAuthorizationPolicyProvider>(policyProvider);

0 commit comments

Comments
 (0)