|
| 1 | +--- |
| 2 | +uid: migrating-from-1-to-2 |
| 3 | +title: Migrating from 1.x to 2.x |
| 4 | +--- |
| 5 | + |
| 6 | +# Migrating from bUnit 1.x to 2.x |
| 7 | +This document describes the changes made in bUnit 2.x that may affect existing tests written for bUnit 1.x. The old documentation for bUnit 1.x is available at: https://v1.bunit.dev. |
| 8 | + |
| 9 | +## One package to rule them all |
| 10 | +`bunit.core` and `bunit.web` have be merged into a single package called `bunit`. The seperation was used to allow for extensibitlity, which isn't used anymore. Therefore `bunit.core` and `bunit.web` will stay on version 1.x, while `bunit` will be the only package going forward. To migrate, simply remove the `bunit.core` and `bunit.web` packages and add the `bunit` package. We don't expect many users to have used the `bunit.core` or `bunit.web` package directly, but may hit 3rd party packages that depend on them. |
| 11 | + |
| 12 | +## `TestContext` renamed to `BunitContext` |
| 13 | + |
| 14 | +The `TestContext` class has been renamed to `BunitContext`. To migrate tests, rename all instances of `TestContext` to `BunitContext`: |
| 15 | + |
| 16 | +```diff |
| 17 | +- public class MyTestClass : TestContext |
| 18 | ++ public class MyTestClass : BunitContext |
| 19 | +``` |
| 20 | + |
| 21 | +## `BunitContext` offers `Dispose` and `DisposeAsync` methods |
| 22 | +In version 1, the `TestContext` class only implemented `IDisposable`, but in version 2, it also implements `IAsyncDisposable`. Therefore, if there are asynchronous disposable inside the container for example, the asynchronous version should be used. |
| 23 | + |
| 24 | +## The `Fake` prefix was renamed to `Bunit` (e.g. `FakeNavigationManager` to `BunitNavigationManager`) |
| 25 | +The `Fake` prefix used for various fake implementations has been renamed to `Bunit`. This includes the following types: |
| 26 | + * `FakeNavigationManager` to `BunitNavigationManager` |
| 27 | + * `FakeJSRuntime` to `BunitJSRuntime` |
| 28 | + * `FakeAuthenticationStateProvider` to `BunitAuthenticationStateProvider` |
| 29 | + * `FakeAuthrozitationContext` to `BunitAuthorizationContext` |
| 30 | + * `FakeuthorizationPolicyProvider` to `BunitAuthorizationPolicyProvider` |
| 31 | + |
| 32 | +## Unified the `Render` methods |
| 33 | +In v1 there were multiple `RenderXXX`methods (like `RenderComponent`, `Render` and `SetParametersAndRender`) that were used to render components and markup. In v2, these methods have been unified into a single `Render` method that can handle both components and markup) via the simple `Render` method: |
| 34 | + |
| 35 | +```diff |
| 36 | +- var cut = RenderComponent<MyComponent>(); |
| 37 | ++ var cut = Render<MyComponent>(); |
| 38 | +``` |
| 39 | + |
| 40 | +## Removed some abstraction |
| 41 | +Many types were removed in favor of one public type: |
| 42 | + * `IRenderedComponent<TComponent>` - where `TComponent` is the type of the component rendered (or the a `ContainerFragment` if markup was rendered). |
| 43 | + |
| 44 | +The removed types were: |
| 45 | + * `IRenderedFragment` |
| 46 | + * `IRenderedComponent` |
| 47 | + * `IRenderedMarkup` |
| 48 | + * `IRenderedComponentBase` |
| 49 | + |
| 50 | +If any of the types were used (for example in extensions methods), they should be replaced with `IRenderedComponent`. |
| 51 | + |
| 52 | +## Removed of `IsNullOrEmpty` extension method on `IEnumerable<T>` and `CreateLogger` on `IServiceProvider` |
| 53 | +The `IsNullOrEmpty` extension method on `IEnumerable<T>` has been removed, as well as the `CreateLogger` extension method on `IServiceProvider`. These extension methods are pretty common and conflict with other libraries. These methods can be recreated like this: |
| 54 | + |
| 55 | +```csharp |
| 56 | +public static class Extensions |
| 57 | +{ |
| 58 | + public static bool IsNullOrEmpty<T>(this IEnumerable<T> enumerable) |
| 59 | + => enumerable == null || !enumerable.Any(); |
| 60 | + |
| 61 | + public static ILogger<T> CreateLogger<T>(this IServiceProvider serviceProvider) |
| 62 | + { |
| 63 | + var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>() ?? NullLoggerFactory.Instance; |
| 64 | + return loggerFactory.CreateLogger<T>(); |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
0 commit comments