This document describes the changes that need to be made to migrate from bUnit 1.x to 2.x.
The GetChangesSinceFirstRender and GetChangesSinceLastRender methods have been removed from RenderedComponent<TComponent>. There is no one-to-one replacement for these methods, but the general idea is to select the HTML in question via Find and assert against that.
Alternatively, the RenderedFragment still offers the OnMarkupUpdated event, which can be used to assert against the markup after a render.
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:
public static class Extensions
{
public static bool IsNullOrEmpty<T>(this IEnumerable<T> enumerable)
=> enumerable == null || !enumerable.Any();
public static ILogger<T> CreateLogger<T>(this IServiceProvider serviceProvider)
{
var loggerFactory = serviceProvider.GetRequiredService<ILoggerFactory>() ?? NullLoggerFactory.Instance;
return loggerFactory.CreateLogger<T>();
}
}The bunit.core and bunit.web packages have been merged into a single bunit package. If you used either of these packages, you should remove them and install the bunit package instead.
IRenderedComponentBase<TComponent>, IRenderedComponent<TComponent>, RenderedFragmentBase, RenderedFragment and RenderedFragmentBase have been removed.
If you used either of these types, you should replace them with RenderedComponent<TComponent> or RenderedFragment respectively.
The WebTestRender class has been merged into the TestRender class. If you used WebTestRender, you should replace it with BunitTestRender.
The Fake prefix has been replaced with Bunit in many test doubles. For example, FakeNavigationManager is now BunitNavigationManager. If you reference any of these types explicitly, you need to update your code.
The AddTestAuthorization method on BunitContext has been renamed to AddAuthorization. If you used AddTestAuthorization, you should replace it with AddAuthorization.
The BunitContext and BunitContextBase classes have been merged into a single BunitContext class. All references to BunitContextBase should replace them with BunitContext to migrate.
To make the API more consistent, RenderComponent and SetParametersAndRender methods have been renamed to Render.
Using ComponentParameter and factory methods to create them is not recommend in V1 and have now been removed in V2. Instead, use the strongly typed builder pattern that enables you to pass parameters to components you render.
The BunitContext now implements IDisposable and IAsyncDisposable. In version 1.x, BunitContext only implemented IDisposable and cleaned up asynchronous objects in the synchronous Dispose method. This is no longer the case, and asynchronous objects are now cleaned up in the DisposeAsync method.
If you register services into the container that implement IAsyncDisposable make sure that the test framework calls the right method.
The TestContext class has been renamed to BunitContext. If you used TestContext, you should replace it with BunitContext.
The TestContextWrapper class has been removed. Either use lifecycle events of the testing framework (like LifeCycle.InstancePerTestCase in NUnit).
[FixtureLifeCycle(LifeCycle.InstancePerTestCase)]
public class HelloWorldInstancePerTestCase : Bunit.TestContext
{
[Test]
public void HelloWorldComponentRendersCorrectly()
{
// Act
var cut = RenderComponent<HelloWorld>();
// Assert
cut.MarkupMatches("<h1>Hello world from Blazor</h1>");
}
}Or use the BunitContext directly and manage the lifecycle yourself.
The TestServiceProvider class has been renamed to BunitTestServiceProvider. If you used TestServiceProvider, you should replace it with BunitTestServiceProvider.
DisposeComponentsAsync allows to await DisposeAsync of components under test. If you used DisposeComponents, you should replace it with DisposeComponentsAsync.
The IRefreshableElementCollection interface has been removed. With this the FindAll method does not accept a bool enableRefresh parameter anymore. Code like this:
var items = cut.FindAll("li", enableRefresh: true);
cut.Find("button").Click(); // Some action that causes items to change
Assert.Equal(3, items.Count);Should be changed to this:
var items = cut.FindAll("li");
cut.Find("button").Click(); // Some action that causes items to change
items = cut.FindAll("li"); // Re-query the items
Assert.Equal(3, items.Count);