Skip to content

Commit 13eb090

Browse files
committed
Updated change log, removed redundant code
1 parent a7d098c commit 13eb090

2 files changed

Lines changed: 53 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,58 @@ List of new features.
2727

2828
By [@egil](https://github.com/egil) in [#260](https://github.com/egil/bUnit/pull/260).
2929

30+
- Added `Render(RenderFragment)` and `Render<TComponent>(RenderFragment)` methods to `TestContext`, as well as various overloads to the `MarkupMatches` methods, that also takes a `RenderFragment` as the expected value.
31+
32+
The difference between the generic `Render` method and the non-generic one is that the generic returns an `IRenderedComponent<TComponent>`, whereas the non-generic one returns a `IRenderedFragment`.
33+
34+
Calling `Render<TComponent>(RenderFragent)` is equivalent to calling `Render(RenderFragment).FindComponent<TComponent>()`, e.g. it returns the first component in the render tree of type `TComponent`. This is different from the `RenderComponent<TComponent>()` method, where `TComponent` _is_ the root component of the render tree.
35+
36+
The main usecase for these are when writing tests inside .razor files. Here the inline syntax for declaring render fragments make these methods very useful.
37+
38+
For example, to tests the `<Counter>` page/component that is part of new Blazor apps, do the following (inside a `CounterTest.razor` file):
39+
40+
```cshtml
41+
@code
42+
{
43+
[Fact]
44+
public void Counter_Increments_When_Button_Is_Clicked()
45+
{
46+
using var ctx = new TestContext();
47+
var cut = ctx.Render(@<Counter />);
48+
49+
cut.Find("button").Click();
50+
51+
cut.Find("p").MarkupMatches(@<p>Current count: 1</p>);
52+
}
53+
}
54+
```
55+
56+
Note: This example uses xUnit, but NUnit or MSTest works equally well.
57+
58+
In addition to the new `Render` methods, a empty `BuildRenderTree` method has been added to the `TestContext` type. This makes it possible to inherit from the `TestContext` type in test components, removing the need for newing up the `TestContext` in each test.
59+
60+
This means the test component above ends up looking like this:
61+
62+
```cshtml
63+
@inherts TestContext
64+
@code
65+
{
66+
[Fact]
67+
public void Counter_Increments_When_Button_Is_Clicked()
68+
{
69+
var cut = Render(@<Counter />);
70+
71+
cut.Find("button").Click();
72+
73+
cut.Find("p").MarkupMatches(@<p>Current count: 1</p>);
74+
}
75+
}
76+
```
77+
78+
Tip: If you have multiple test components in the same folder, you can add a `_Imports.razor` file inside it and add the `@inherits TestContext` statement in that, removing the need to add it to every test component.
79+
80+
By [@egil](https://github.com/egil) in [#262](https://github.com/egil/bUnit/pull/262).
81+
3082
### Changed
3183
List of changes in existing functionality.
3284

src/bunit.core/TestServiceProvider.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@ public sealed class TestServiceProvider : IServiceProvider, IServiceCollection,
1313
{
1414
private readonly IServiceCollection _serviceCollection;
1515
private ServiceProvider? _serviceProvider;
16-
17-
/// <summary>
18-
/// Gets a reusable default test service provider.
19-
/// </summary>
20-
public static readonly IServiceProvider Default = new TestServiceProvider(new ServiceCollection(), true);
21-
16+
2217
/// <summary>
2318
/// Gets whether this <see cref="TestServiceProvider"/> has been initialized, and
2419
/// no longer will accept calls to the <c>AddService</c>'s methods.

0 commit comments

Comments
 (0)