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: CHANGELOG.md
+52Lines changed: 52 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,6 +27,58 @@ List of new features.
27
27
28
28
By [@egil](https://github.com/egil) in [#260](https://github.com/egil/bUnit/pull/260).
29
29
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()
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()
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).
0 commit comments