Skip to content

Commit 580eca6

Browse files
committed
refactor: Merge TestContext with TestContextBase
1 parent c5402c4 commit 580eca6

25 files changed

+373
-409
lines changed

MIGRATION.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ The `Fake` prefix has been replaced with `Bunit` in many test doubles. For examp
3939

4040
### Renamed `AddTestAuthorization` to `AddAuthorization`
4141
The `AddTestAuthorization` method on `TestContext` has been renamed to `AddAuthorization`. If you used `AddTestAuthorization`, you should replace it with `AddAuthorization`.
42+
43+
## Merged `TestContext` and `TestContextBase`
44+
The `TestContext` and `TestContextBase` classes have been merged into a single `TestContext` class. All references to `TestContextBase` should replace them with `TestContext` to migrate.

docs/samples/tests/xunit/CustomServiceProviderFactoryUsage.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ void ConfigureContainer(ContainerBuilder containerBuilder)
4949

5050
//get a service which was installed in the bUnit ServiceCollection
5151

52-
var testContextBase = Services.GetService<TestContextBase>();
52+
var testContext = Services.GetService<TestContext>();
5353

54-
Assert.NotNull(testContextBase);
55-
Assert.Equal(this, testContextBase);
54+
Assert.NotNull(testContext);
55+
Assert.Equal(this, testContext);
5656
}
5757

5858
[Fact]
@@ -81,9 +81,9 @@ ILifetimeScope ConfigureContainer(IServiceCollection services)
8181

8282
//get a service which was installed in the bUnit ServiceCollection
8383

84-
var testContextBase = Services.GetService<TestContextBase>();
84+
var testContext = Services.GetService<TestContext>();
8585

86-
Assert.NotNull(testContextBase);
87-
Assert.Equal(this, testContextBase);
86+
Assert.NotNull(testContext);
87+
Assert.Equal(this, testContext);
8888
}
8989
}

docs/site/docs/interaction/dispose-components.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ title: Disposing components and their children
44
---
55

66
# Disposing components
7-
To dispose all components rendered with a `TestContext`, use the [`DisposeComponents`](xref:Bunit.TestContextBase.DisposeComponents) method. Calling this method will dispose all rendered components, calling any `IDisposable.Dispose` or/and `IAsyncDisposable.DisposeAsync` methods they might have, and remove the components from the render tree, starting with the root components and then walking down the render tree to all the child components.
7+
To dispose all components rendered with a `TestContext`, use the [`DisposeComponents`](xref:Bunit.TestContext.DisposeComponents) method. Calling this method will dispose all rendered components, calling any `IDisposable.Dispose` or/and `IAsyncDisposable.DisposeAsync` methods they might have, and remove the components from the render tree, starting with the root components and then walking down the render tree to all the child components.
88

99
Disposing rendered components enables testing of logic in `Dispose` methods, e.g., event handlers, that should be detached to avoid memory leaks.
1010

@@ -16,7 +16,7 @@ The following example of this:
1616
> For `IAsyncDisposable` (since .net5) relying on [`WaitForState()`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForState(Bunit.IRenderedFragment,Func{System.Boolean},System.Nullable{TimeSpan})) or [`WaitForAssertion()`](xref:Bunit.RenderedFragmentWaitForHelperExtensions.WaitForAssertion(Bunit.IRenderedFragment,Action,System.Nullable{TimeSpan})) will not work as a disposed component will not trigger a new render cycle.
1717
1818
## Checking for exceptions
19-
`Dispose` as well as `DisposeAsync` can throw exceptions which can be asserted as well. If a component under test throws an exception in `Dispose` the [`DisposeComponents`](xref:Bunit.TestContextBase.DisposeComponents) will throw the exception to the user code:
19+
`Dispose` as well as `DisposeAsync` can throw exceptions which can be asserted as well. If a component under test throws an exception in `Dispose` the [`DisposeComponents`](xref:Bunit.TestContext.DisposeComponents) will throw the exception to the user code:
2020

2121
[!code-csharp[](../../../samples/tests/xunit/DisposeComponentsTest.cs#L28-L32)]
2222

docs/site/docs/providing-input/root-render-tree.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ title: Controlling the root render tree
77

88
The root render tree, the render tree in which components under test are rendered, can be added to before components are rendered with bUnit's test context. This is mostly useful when a component under test, or a component it depends on, must be rendered inside another component, that provides a cascading value to it.
99

10-
For example, when using Blazor’s authentication, it is common to add the `CascadingAuthenticationState` component higher up the render tree, such that it can provide authentication state to those components below it that need it. Adding this through the <xref:Bunit.TestContextBase.RenderTree> property on the <xref:Bunit.TestContextBase> type makes it possible to add it once in a shared setup method, and not have to do so in every test method during the call to [`RenderComponent()`](xref:Bunit.Rendering.RootRenderTree.Add``1(System.Action{Bunit.ComponentParameterCollectionBuilder{``0}})).
10+
For example, when using Blazor’s authentication, it is common to add the `CascadingAuthenticationState` component higher up the render tree, such that it can provide authentication state to those components below it that need it. Adding this through the <xref:Bunit.TestContext.RenderTree> property on the <xref:Bunit.TestContext> type makes it possible to add it once in a shared setup method, and not have to do so in every test method during the call to [`RenderComponent()`](xref:Bunit.Rendering.RootRenderTree.Add``1(System.Action{Bunit.ComponentParameterCollectionBuilder{``0}})).
1111

1212
This can also be useful when writing tests that use a third-party component library, that require a special root component to be added to the render tree, but which otherwise don’t change between tests.
1313

docs/site/docs/test-doubles/auth.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ The test implementation of Blazor's authentication and authorization can be put
1515
- **Authenticated** and **authorized**
1616
- **Authenticated** and **authorized** with one or more **roles**, **claims**, and/or **policies**
1717

18-
bUnit's authentication and authorization implementation is easily available by calling [`AddAuthorization()`](xref:Bunit.TestDoubles.BunitAuthorizationExtensions.AddAuthorization(Bunit.TestContextBase)) on a test context. This adds the necessary services to the `Services` collection and the `CascadingAuthenticationState` component to the [root render tree](xref:root-render-tree). The method returns an instance of the <xref:Bunit.TestDoubles.TestAuthorizationContext> type that allows you to control the authentication and authorization state for a test.
18+
bUnit's authentication and authorization implementation is easily available by calling [`AddAuthorization()`](xref:Bunit.TestDoubles.BunitAuthorizationExtensions.AddAuthorization(Bunit.TestContext)) on a test context. This adds the necessary services to the `Services` collection and the `CascadingAuthenticationState` component to the [root render tree](xref:root-render-tree). The method returns an instance of the <xref:Bunit.TestDoubles.TestAuthorizationContext> type that allows you to control the authentication and authorization state for a test.
1919

2020
> [!NOTE]
21-
> If your test class inherits directly from bUnit's <xref:Bunit.TestContext> then you need to call the [`AddAuthorization()`](xref:Bunit.TestDoubles.BunitAuthorizationExtensions.AddAuthorization(Bunit.TestContextBase)) method on `this`, since `AddAuthorization()` is an extension method, otherwise it wont be available. E.g.: `this.AddAuthorization()`.
21+
> If your test class inherits directly from bUnit's <xref:Bunit.TestContext> then you need to call the [`AddAuthorization()`](xref:Bunit.TestDoubles.BunitAuthorizationExtensions.AddAuthorization(Bunit.TestContext)) method on `this`, since `AddAuthorization()` is an extension method, otherwise it wont be available. E.g.: `this.AddAuthorization()`.
2222
2323
The following sections show how to set each of these states in a test.
2424

src/bunit/ComponentFactories/InstanceComponentFactory{TComponent}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public IComponent Create(Type componentType)
1818
{
1919
throw new InvalidOperationException(
2020
$"The instance object passed to the" +
21-
$"{nameof(TestContextBase.ComponentFactories)}.{nameof(ComponentFactoryCollectionExtensions.Add)}<{typeof(TComponent).Name}>(instance) method can only be used to replace " +
21+
$"{nameof(TestContext.ComponentFactories)}.{nameof(ComponentFactoryCollectionExtensions.Add)}<{typeof(TComponent).Name}>(instance) method can only be used to replace " +
2222
$"one {typeof(TComponent)} component in the render tree.");
2323
}
2424

src/bunit/Extensions/Internal/AngleSharpExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,17 +65,17 @@ public static HtmlComparer GetHtmlComparer(this INodeList nodes)
6565
/// owning context, if one is available.
6666
/// </summary>
6767
/// <returns>The <see cref="TestContext"/> or null if not found.</returns>
68-
public static TestContextBase? GetTestContext(this INode? node)
68+
public static TestContext? GetTestContext(this INode? node)
6969
{
70-
return node?.Owner?.Context.GetService<TestContextBase>();
70+
return node?.Owner?.Context.GetService<TestContext>();
7171
}
7272

7373
/// <summary>
7474
/// Gets the <see cref="TestContext"/> stored in the <paramref name="nodes"/>s
7575
/// owning context, if one is available.
7676
/// </summary>
7777
/// <returns>The <see cref="TestContext"/> or null if not found.</returns>
78-
public static TestContextBase? GetTestContext(this INodeList nodes)
78+
public static TestContext? GetTestContext(this INodeList nodes)
7979
{
8080
return nodes?.Length > 0 ? nodes[0].GetTestContext() : null;
8181
}

src/bunit/Extensions/RenderedComponentRenderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static void SetParametersAndRender<TComponent>(this IRenderedComponent<TC
2828
{
2929
ArgumentNullException.ThrowIfNull(renderedComponent);
3030

31-
var renderer = (TestRenderer)renderedComponent.Services.GetRequiredService<TestContextBase>().Renderer;
31+
var renderer = (TestRenderer)renderedComponent.Services.GetRequiredService<TestContext>().Renderer;
3232

3333
try
3434
{

src/bunit/Extensions/RenderedFragmentExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static IRenderedComponent<TComponent> FindComponent<TComponent>(this IRen
5555
{
5656
ArgumentNullException.ThrowIfNull(renderedFragment);
5757

58-
var renderer = renderedFragment.Services.GetRequiredService<TestContextBase>().Renderer;
58+
var renderer = renderedFragment.Services.GetRequiredService<TestContext>().Renderer;
5959
return renderer.FindComponent<TComponent>(renderedFragment);
6060
}
6161

@@ -70,7 +70,7 @@ public static IReadOnlyList<IRenderedComponent<TComponent>> FindComponents<TComp
7070
{
7171
ArgumentNullException.ThrowIfNull(renderedFragment);
7272

73-
var renderer = renderedFragment.Services.GetRequiredService<TestContextBase>().Renderer;
73+
var renderer = renderedFragment.Services.GetRequiredService<TestContext>().Renderer;
7474
var components = renderer.FindComponents<TComponent>(renderedFragment);
7575

7676
return components.OfType<IRenderedComponent<TComponent>>().ToArray();

src/bunit/Extensions/RenderedFragmentInvokeAsyncExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static Task InvokeAsync(this IRenderedFragment renderedFragment, Action w
1919

2020
return renderedFragment
2121
.Services
22-
.GetRequiredService<TestContextBase>()
22+
.GetRequiredService<TestContext>()
2323
.Renderer
2424
.Dispatcher
2525
.InvokeAsync(workItem);
@@ -37,7 +37,7 @@ public static Task InvokeAsync(this IRenderedFragment renderedFragment, Func<Tas
3737

3838
return renderedFragment
3939
.Services
40-
.GetRequiredService<TestContextBase>()
40+
.GetRequiredService<TestContext>()
4141
.Renderer
4242
.Dispatcher
4343
.InvokeAsync(workItem);
@@ -55,7 +55,7 @@ public static Task<T> InvokeAsync<T>(this IRenderedFragment renderedFragment, Fu
5555

5656
return renderedFragment
5757
.Services
58-
.GetRequiredService<TestContextBase>()
58+
.GetRequiredService<TestContext>()
5959
.Renderer
6060
.Dispatcher
6161
.InvokeAsync(workItem);
@@ -73,7 +73,7 @@ public static Task<T> InvokeAsync<T>(this IRenderedFragment renderedFragment, Fu
7373

7474
return renderedFragment
7575
.Services
76-
.GetRequiredService<TestContextBase>()
76+
.GetRequiredService<TestContext>()
7777
.Renderer
7878
.Dispatcher
7979
.InvokeAsync(workItem);

0 commit comments

Comments
 (0)