You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/site/docs/providing-input/inject-services-into-components.md
+44-1Lines changed: 44 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,4 +5,47 @@ title: Injecting Services into Components Under Test
5
5
6
6
# Injecting Services into Components Under Test
7
7
8
-
TODO!
8
+
It is common that components under test has a dependency on services, injected into them through the `@inject IMyService MyService` syntax in .razor files, or the `[Inject] private IMyService MyService { get; set; }` syntax in .cs files.
9
+
10
+
This is supported in bUnit through the `Services` collection on the `ITestContext` types that is used in both C# and Razor based tests. It allows services to be registered, how it is done in production code in `Startup.cs` in Blazor Server projects and in `Program.cs` in Blazor Wasm projects.
11
+
12
+
In bUnit, you register the services in the `Services` collection _before_ you render a component under test.
13
+
14
+
The following sections shows how to do this in C# and Razor based tests. The examples will test the `<WeatherForecasts>` component listed below, that depends on the `IWeatherForecastService` service, injected in line 1:
Here is a C# based test that registers the `IWeatherForecastService` in the `Services` collection, which is needed by the `<WeatherForecasts>` component listed above.
The highlighted line shows how the `IWeatherForecastService` is registered in the test context's `Services` collection, which is just a standard [`IServiceCollection`](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.iservicecollection), using the standard .NET Core DI services method, [`AddSingleton`](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.servicecollectionserviceextensions.addsingleton?view=dotnet-plat-ext-3.1#Microsoft_Extensions_DependencyInjection_ServiceCollectionServiceExtensions_AddSingleton__1_Microsoft_Extensions_DependencyInjection_IServiceCollection___0_).
25
+
26
+
> [!NOTE]
27
+
> The `AddSingleton()` method is only available on the `Services` collection if you import the `Microsoft.Extensions.DependencyInjection` type.
28
+
29
+
## Injecting Services in Razor Based Tests
30
+
31
+
Here is a Razor based test that registers the `IWeatherForecastService` in the `Services` collection during the `Setup` methods, which is needed by the `<WeatherForecasts>` component listed above.
The highlighted line shows how the `IWeatherForecastService` is registered, using the standard .NET Core DI services method, [`AddSingleton`](https://docs.microsoft.com/en-us/dotnet/api/microsoft.extensions.dependencyinjection.servicecollectionserviceextensions.addsingleton?view=dotnet-plat-ext-3.1#Microsoft_Extensions_DependencyInjection_ServiceCollectionServiceExtensions_AddSingleton__1_Microsoft_Extensions_DependencyInjection_IServiceCollection___0_).
36
+
37
+
This can either be done via the `Fixture`'s `Setup` method as in this example, if you want to separate the service registration from the test method, or it can be done in the test method _before_ calling `GetComponentUnderTest()`.
38
+
39
+
In the following example shows how to do this with `<SnapshotTest>` tests:
0 commit comments