diff --git a/MIGRATION.md b/MIGRATION.md index 7a4a6ab88..05b451446 100644 --- a/MIGRATION.md +++ b/MIGRATION.md @@ -76,3 +76,6 @@ public class HelloWorldInstancePerTestCase : Bunit.TestContext ``` Or use the `BunitContext` directly and manage the lifecycle yourself. + +## `TestServiceProvider` renamed to `BunitTestServiceProvider` +The `TestServiceProvider` class has been renamed to `BunitTestServiceProvider`. If you used `TestServiceProvider`, you should replace it with `BunitTestServiceProvider`. diff --git a/docs/samples/tests/xunit/MockHttpClientBunitHelpers.cs b/docs/samples/tests/xunit/MockHttpClientBunitHelpers.cs index 0a1a66b33..e566e9406 100644 --- a/docs/samples/tests/xunit/MockHttpClientBunitHelpers.cs +++ b/docs/samples/tests/xunit/MockHttpClientBunitHelpers.cs @@ -11,7 +11,7 @@ namespace Bunit.Docs.Samples; public static class MockHttpClientBunitHelpers { - public static MockHttpMessageHandler AddMockHttpClient(this TestServiceProvider services) + public static MockHttpMessageHandler AddMockHttpClient(this BunitServiceProvider services) { var mockHttpHandler = new MockHttpMessageHandler(); var httpClient = mockHttpHandler.ToHttpClient(); diff --git a/docs/site/docs/providing-input/inject-services-into-components.md b/docs/site/docs/providing-input/inject-services-into-components.md index ffca9d178..405713d53 100644 --- a/docs/site/docs/providing-input/inject-services-into-components.md +++ b/docs/site/docs/providing-input/inject-services-into-components.md @@ -28,11 +28,11 @@ The highlighted line shows how the `IWeatherForecastService` is registered in th ## Fallback service provider -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. +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. ### When is the fallback service provider used? -The logic inside the `TestServiceProvider` for using the fallback service provider is as follows: +The logic inside the `BunitServiceProvider` for using the fallback service provider is as follows: 1. Try resolving the requested service from the standard service provider in bUnit. 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: In this example, the `DummyService` is provided by the fallback service provider, since it is not registered in the default service provider. ## Using a custom IServiceProvider implementation -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. +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. ### Registering Autofac service provider factory 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. diff --git a/src/bunit/Asserting/MarkupMatchesAssertExtensions.cs b/src/bunit/Asserting/MarkupMatchesAssertExtensions.cs index 61e3a4132..b4e4f1244 100644 --- a/src/bunit/Asserting/MarkupMatchesAssertExtensions.cs +++ b/src/bunit/Asserting/MarkupMatchesAssertExtensions.cs @@ -275,7 +275,7 @@ public static void MarkupMatches(this RenderedFragment actual, RenderFragment ex // TODO: This will be obsolete with: https://github.com/bUnit-dev/bUnit/issues/1018 // As the renderer would be transient we don't have to new up an instance using var renderer = new BunitRenderer( - actual.Services.GetRequiredService(), + actual.Services.GetRequiredService(), actual.Services.GetRequiredService()); var renderedFragment = renderer.RenderFragment(expected); MarkupMatches(actual, renderedFragment, userMessage); diff --git a/src/bunit/BunitContext.cs b/src/bunit/BunitContext.cs index 1864e6c61..b7241cbe6 100644 --- a/src/bunit/BunitContext.cs +++ b/src/bunit/BunitContext.cs @@ -33,7 +33,7 @@ public partial class BunitContext : IDisposable, IAsyncDisposable /// Gets the service collection and service provider that is used when a /// component is rendered by the test context. /// - public TestServiceProvider Services { get; } + public BunitServiceProvider Services { get; } /// /// Gets the that all components rendered with the @@ -58,7 +58,7 @@ public partial class BunitContext : IDisposable, IAsyncDisposable /// public BunitContext() { - Services = new TestServiceProvider(); + Services = new BunitServiceProvider(); Services.AddSingleton(_ => ComponentFactories); Services.AddDefaultBunitContextServices(this, JSInterop); } diff --git a/src/bunit/TestServiceProvider.cs b/src/bunit/BunitServiceProvider.cs similarity index 93% rename from src/bunit/TestServiceProvider.cs rename to src/bunit/BunitServiceProvider.cs index 4afcb176d..0748b95ec 100644 --- a/src/bunit/TestServiceProvider.cs +++ b/src/bunit/BunitServiceProvider.cs @@ -6,7 +6,7 @@ namespace Bunit; /// Represents a and /// as a single type used for test purposes. /// -public sealed class TestServiceProvider : IKeyedServiceProvider, IServiceCollection, IDisposable, IAsyncDisposable +public sealed class BunitServiceProvider : IKeyedServiceProvider, IServiceCollection, IDisposable, IAsyncDisposable { private static readonly ServiceProviderOptions DefaultServiceProviderOptions = new() { ValidateScopes = true }; private readonly IServiceCollection serviceCollection; @@ -18,7 +18,7 @@ public sealed class TestServiceProvider : IKeyedServiceProvider, IServiceCollect private Func serviceProviderFactory; /// - /// Gets a value indicating whether this has been initialized, and + /// Gets a value indicating whether this has been initialized, and /// no longer will accept calls to the AddService's methods. /// public bool IsProviderInitialized => serviceProvider is not null; @@ -50,15 +50,15 @@ public ServiceProviderOptions Options } /// - /// Initializes a new instance of the class + /// Initializes a new instance of the class /// and sets its service collection to the provided , if any. /// - public TestServiceProvider(IServiceCollection? initialServiceCollection = null) + public BunitServiceProvider(IServiceCollection? initialServiceCollection = null) : this(initialServiceCollection ?? new ServiceCollection(), initializeProvider: false) { } - private TestServiceProvider(IServiceCollection initialServiceCollection, bool initializeProvider) + private BunitServiceProvider(IServiceCollection initialServiceCollection, bool initializeProvider) { serviceCollection = initialServiceCollection; serviceProviderFactory = () => serviceCollection.BuildServiceProvider(Options); @@ -110,7 +110,7 @@ private void InitializeProvider() { CheckInitializedAndThrow(); - serviceCollection.AddSingleton(this); + serviceCollection.AddSingleton(this); rootServiceProvider = serviceProviderFactory.Invoke(); serviceScope = rootServiceProvider.CreateScope(); serviceProvider = serviceScope.ServiceProvider; diff --git a/src/bunit/Extensions/TestServiceProviderExtensions.cs b/src/bunit/Extensions/BunitServiceProviderExtensions.cs similarity index 97% rename from src/bunit/Extensions/TestServiceProviderExtensions.cs rename to src/bunit/Extensions/BunitServiceProviderExtensions.cs index baa029fa7..4b89b03bb 100644 --- a/src/bunit/Extensions/TestServiceProviderExtensions.cs +++ b/src/bunit/Extensions/BunitServiceProviderExtensions.cs @@ -12,7 +12,7 @@ namespace Bunit.Extensions; /// /// Helper methods for correctly registering test dependencies. /// -public static class TestServiceProviderExtensions +public static class BunitServiceProviderExtensions { /// /// Registers the default services required by the web . diff --git a/src/bunit/Rendering/BunitRenderer.cs b/src/bunit/Rendering/BunitRenderer.cs index d91d67d1b..ef9cdfb16 100644 --- a/src/bunit/Rendering/BunitRenderer.cs +++ b/src/bunit/Rendering/BunitRenderer.cs @@ -10,7 +10,7 @@ namespace Bunit.Rendering; /// public sealed class BunitRenderer : Renderer { - private readonly TestServiceProvider services; + private readonly BunitServiceProvider services; [UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_isBatchInProgress")] private static extern ref bool GetIsBatchInProgressField(Renderer renderer); @@ -57,7 +57,7 @@ private bool IsBatchInProgress /// /// Initializes a new instance of the class. /// - public BunitRenderer(TestServiceProvider services, ILoggerFactory loggerFactory) + public BunitRenderer(BunitServiceProvider services, ILoggerFactory loggerFactory) : base(services, loggerFactory, new BunitComponentActivator(services.GetRequiredService(), null)) { this.services = services; @@ -68,7 +68,7 @@ public BunitRenderer(TestServiceProvider services, ILoggerFactory loggerFactory) /// /// Initializes a new instance of the class. /// - public BunitRenderer(TestServiceProvider services, ILoggerFactory loggerFactory, IComponentActivator componentActivator) + public BunitRenderer(BunitServiceProvider services, ILoggerFactory loggerFactory, IComponentActivator componentActivator) : base(services, loggerFactory, new BunitComponentActivator(services.GetRequiredService(), componentActivator)) { this.services = services; diff --git a/src/bunit/Rendering/RenderedFragment.cs b/src/bunit/Rendering/RenderedFragment.cs index dc7efbeb0..3f817d2a6 100644 --- a/src/bunit/Rendering/RenderedFragment.cs +++ b/src/bunit/Rendering/RenderedFragment.cs @@ -10,7 +10,7 @@ namespace Bunit; [DebuggerDisplay("Rendered:{RenderCount}")] public class RenderedFragment : IDisposable { - [SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "Owned by TestServiceProvider, disposed by it.")] + [SuppressMessage("Usage", "CA2213:Disposable fields should be disposed", Justification = "Owned by BunitServiceProvider, disposed by it.")] private readonly BunitHtmlParser htmlParser; private string markup = string.Empty; private INodeList? latestRenderNodes; diff --git a/src/bunit/TestDoubles/Authorization/BunitAuthorizationContext.cs b/src/bunit/TestDoubles/Authorization/BunitAuthorizationContext.cs index 0667cf429..25942ac0a 100644 --- a/src/bunit/TestDoubles/Authorization/BunitAuthorizationContext.cs +++ b/src/bunit/TestDoubles/Authorization/BunitAuthorizationContext.cs @@ -53,7 +53,7 @@ public class BunitAuthorizationContext /// /// Creates a new instance of . /// - internal BunitAuthorizationContext(TestServiceProvider services) + internal BunitAuthorizationContext(BunitServiceProvider services) { services.AddSingleton(authService); services.AddSingleton(policyProvider); diff --git a/src/bunit/TestDoubles/Authorization/BunitAuthorizationExtensions.cs b/src/bunit/TestDoubles/Authorization/BunitAuthorizationExtensions.cs index 738b5abb8..cb7c5ad83 100644 --- a/src/bunit/TestDoubles/Authorization/BunitAuthorizationExtensions.cs +++ b/src/bunit/TestDoubles/Authorization/BunitAuthorizationExtensions.cs @@ -7,7 +7,7 @@ namespace Bunit; public partial class BunitContext { /// - /// Adds the appropriate Blazor authentication and authorization services to the to enable + /// Adds the appropriate Blazor authentication and authorization services to the to enable /// an authenticated user, as well as adding the component to the /// test contexts render tree. /// diff --git a/src/bunit/TestDoubles/PersistentComponentState/BunitPersistentComponentState.cs b/src/bunit/TestDoubles/PersistentComponentState/BunitPersistentComponentState.cs index 73ea41590..e3c9bcaad 100644 --- a/src/bunit/TestDoubles/PersistentComponentState/BunitPersistentComponentState.cs +++ b/src/bunit/TestDoubles/PersistentComponentState/BunitPersistentComponentState.cs @@ -38,7 +38,7 @@ internal BunitPersistentComponentState(IServiceProvider services) /// /// Only call this method after all services has been registered with the . /// Calling this method will lookup dependencies of the - /// from the , which means no other services can be registered after this point. + /// from the , which means no other services can be registered after this point. /// public void TriggerOnPersisting() { @@ -52,7 +52,7 @@ public void TriggerOnPersisting() /// /// Only call this method after all services has been registered with the . /// Calling this method will lookup dependencies of the - /// from the , which means no other services can be registered after this point. + /// from the , which means no other services can be registered after this point. /// /// The type. /// The key to use to persist the state. @@ -72,7 +72,7 @@ public void Persist(string key, TValue instance) /// /// Only call this method after all services has been registered with the . /// Calling this method will lookup dependencies of the - /// from the , which means no other services can be registered after this point. + /// from the , which means no other services can be registered after this point. /// /// The key used to persist the instance. /// The persisted instance. diff --git a/tests/bunit.tests/TestServiceProviderTest.cs b/tests/bunit.tests/BunitServiceProviderTests.cs similarity index 93% rename from tests/bunit.tests/TestServiceProviderTest.cs rename to tests/bunit.tests/BunitServiceProviderTests.cs index 75facc538..91d88a8dd 100644 --- a/tests/bunit.tests/TestServiceProviderTest.cs +++ b/tests/bunit.tests/BunitServiceProviderTests.cs @@ -2,12 +2,12 @@ namespace Bunit; -public partial class TestServiceProviderTest +public partial class BunitServiceProviderTests { [Fact(DisplayName = "Provider initialized without a service collection has zero services by default")] public void Test001() { - using var sut = new TestServiceProvider(); + using var sut = new BunitServiceProvider(); sut.Count.ShouldBe(0); } @@ -17,7 +17,7 @@ public void Test002() { var services = new ServiceCollection(); services.AddSingleton(new DummyService()); - using var sut = new TestServiceProvider(services); + using var sut = new BunitServiceProvider(services); sut.Count.ShouldBe(1); sut[0].ServiceType.ShouldBe(typeof(DummyService)); @@ -26,7 +26,7 @@ public void Test002() [Fact(DisplayName = "Services can be registered in the provider like a normal service collection")] public void Test010() { - using var sut = new TestServiceProvider(); + using var sut = new BunitServiceProvider(); sut.Add(new ServiceDescriptor(typeof(DummyService), new DummyService())); sut.Insert(0, new ServiceDescriptor(typeof(AnotherDummyService), new AnotherDummyService())); @@ -40,7 +40,7 @@ public void Test010() [Fact(DisplayName = "Services can be removed in the provider like a normal service collection")] public void Test011() { - using var sut = new TestServiceProvider(); + using var sut = new BunitServiceProvider(); var descriptor = new ServiceDescriptor(typeof(DummyService), new DummyService()); var anotherDescriptor = new ServiceDescriptor(typeof(AnotherDummyService), new AnotherDummyService()); var oneMoreDescriptor = new ServiceDescriptor(typeof(OneMoreDummyService), new OneMoreDummyService()); @@ -62,7 +62,7 @@ public void Test011() [Fact(DisplayName = "Misc collection methods works as expected")] public void Test012() { - using var sut = new TestServiceProvider(); + using var sut = new BunitServiceProvider(); var descriptor = new ServiceDescriptor(typeof(DummyService), new DummyService()); var copyToTarget = new ServiceDescriptor[1]; sut.Add(descriptor); @@ -81,7 +81,7 @@ public void Test013() { var descriptor = new ServiceDescriptor(typeof(AnotherDummyService), new AnotherDummyService()); - using var sut = new TestServiceProvider(); + using var sut = new BunitServiceProvider(); sut.AddSingleton(new DummyService()); sut.GetService(); @@ -103,7 +103,7 @@ public void Test013() [Fact(DisplayName = "Registered services can be retrieved from the provider")] public void Test020() { - using var sut = new TestServiceProvider(); + using var sut = new BunitServiceProvider(); var expected = new DummyService(); sut.AddSingleton(expected); @@ -115,7 +115,7 @@ public void Test020() [Fact(DisplayName = "No registered service returns null")] public void Test021() { - using var sut = new TestServiceProvider(); + using var sut = new BunitServiceProvider(); var result = sut.GetService(typeof(DummyService)); @@ -125,7 +125,7 @@ public void Test021() [Fact(DisplayName = "Registered fallback service provider returns value")] public void Test022() { - using var sut = new TestServiceProvider(); + using var sut = new BunitServiceProvider(); sut.AddFallbackServiceProvider(new FallbackServiceProvider()); var result = sut.GetService(typeof(object)); @@ -137,7 +137,7 @@ public void Test022() [Fact(DisplayName = "Register fallback service with null value")] public void Test023() { - using var sut = new TestServiceProvider(); + using var sut = new BunitServiceProvider(); Assert.Throws(() => sut.AddFallbackServiceProvider(null!)); } @@ -146,7 +146,7 @@ public void Test024() { const string exceptionStringResult = "exceptionStringResult"; - using var sut = new TestServiceProvider(); + using var sut = new BunitServiceProvider(); sut.AddSingleton(exceptionStringResult); sut.AddFallbackServiceProvider(new FallbackServiceProvider()); @@ -160,7 +160,7 @@ public void Test024() [Fact(DisplayName = "Latest fallback provider is used")] public void Test025() { - using var sut = new TestServiceProvider(); + using var sut = new BunitServiceProvider(); sut.AddFallbackServiceProvider(new FallbackServiceProvider()); sut.AddFallbackServiceProvider(new AnotherFallbackServiceProvider()); @@ -186,7 +186,7 @@ public void Test030() [Fact(DisplayName = "Can correctly resolve and dispose of scoped disposable service")] public void Test031() { - var sut = new TestServiceProvider(); + var sut = new BunitServiceProvider(); sut.AddScoped(); var disposable = sut.GetService(); @@ -198,7 +198,7 @@ public void Test031() [Fact(DisplayName = "Can correctly resolve and dispose of transient disposable service")] public void Test032() { - var sut = new TestServiceProvider(); + var sut = new BunitServiceProvider(); sut.AddTransient(); var disposable = sut.GetService(); @@ -210,7 +210,7 @@ public void Test032() [Fact(DisplayName = "Can correctly resolve and dispose of singleton disposable service")] public void Test033() { - var sut = new TestServiceProvider(); + var sut = new BunitServiceProvider(); sut.AddSingleton(); var disposable = sut.GetService(); @@ -222,7 +222,7 @@ public void Test033() [Fact(DisplayName = "Validates that all dependencies can be created when the first service is requested, if ServiceProviderOptions.ValidateOnBuild is true")] public void Test035() { - using var sut = new TestServiceProvider(); + using var sut = new BunitServiceProvider(); sut.Options = new ServiceProviderOptions { ValidateOnBuild = true, @@ -238,7 +238,7 @@ public void Test035() [Fact(DisplayName = "Does not validate all dependencies can be created when the first service is requested, if ServiceProviderOptions.ValidateOnBuild is false")] public void Test036() { - using var sut = new TestServiceProvider(); + using var sut = new BunitServiceProvider(); sut.Options = new ServiceProviderOptions { ValidateOnBuild = false, @@ -255,7 +255,7 @@ public void Test036() [Fact(DisplayName = "Does not validate all dependencies can be created when the first service is requested, if no ServiceProviderOptions is provided (backwards compatibility)")] public void Test037() { - using var sut = new TestServiceProvider(); + using var sut = new BunitServiceProvider(); sut.AddSingleton(); sut.AddSingleton(); @@ -267,7 +267,7 @@ public void Test037() [Fact(DisplayName = "Test custom service provider factory")] public void Test038() { - using var sut = new TestServiceProvider(); + using var sut = new BunitServiceProvider(); sut.AddSingleton(); var dummyServiceProviderFactory = new DummyServiceProviderFactory(); sut.UseServiceProviderFactory(dummyServiceProviderFactory); @@ -284,7 +284,7 @@ public void Test038() [Fact(DisplayName = "Test custom service provider factory as delegate")] public void Test039() { - using var sut = new TestServiceProvider(); + using var sut = new BunitServiceProvider(); sut.AddSingleton(); DummyServiceProvider dummyServiceProvider = null; sut.UseServiceProviderFactory(x => dummyServiceProvider = new DummyServiceProvider(x));