Skip to content

Commit

Permalink
Updated change log, removed redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
egil committed Nov 23, 2020
1 parent a7d098c commit 13eb090
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
52 changes: 52 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,58 @@ List of new features.

By [@egil](https://github.com/egil) in [#260](https://github.com/egil/bUnit/pull/260).
- 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.

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`.

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.

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.

For example, to tests the `<Counter>` page/component that is part of new Blazor apps, do the following (inside a `CounterTest.razor` file):

```cshtml
@code
{
[Fact]
public void Counter_Increments_When_Button_Is_Clicked()
{
using var ctx = new TestContext();
var cut = ctx.Render(@<Counter />);

cut.Find("button").Click();

cut.Find("p").MarkupMatches(@<p>Current count: 1</p>);
}
}
```

Note: This example uses xUnit, but NUnit or MSTest works equally well.

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.

This means the test component above ends up looking like this:

```cshtml
@inherts TestContext
@code
{
[Fact]
public void Counter_Increments_When_Button_Is_Clicked()
{
var cut = Render(@<Counter />);

cut.Find("button").Click();

cut.Find("p").MarkupMatches(@<p>Current count: 1</p>);
}
}
```

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.

By [@egil](https://github.com/egil) in [#262](https://github.com/egil/bUnit/pull/262).
### Changed
List of changes in existing functionality.

Expand Down
7 changes: 1 addition & 6 deletions src/bunit.core/TestServiceProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ public sealed class TestServiceProvider : IServiceProvider, IServiceCollection,
{
private readonly IServiceCollection _serviceCollection;
private ServiceProvider? _serviceProvider;

/// <summary>
/// Gets a reusable default test service provider.
/// </summary>
public static readonly IServiceProvider Default = new TestServiceProvider(new ServiceCollection(), true);


/// <summary>
/// Gets whether this <see cref="TestServiceProvider"/> has been initialized, and
/// no longer will accept calls to the <c>AddService</c>'s methods.
Expand Down

0 comments on commit 13eb090

Please sign in to comment.