All notable changes to bUnit will be documented in this file. The project adheres to Semantic Versioning.
1.36.0 - 2024-11-12
- Bumped .NET 9 version dependencies to stable packages.
1.35.3 - 2024-11-12
- Extension packages (
bunit.generators
andbunit.web.query
) are flagged as stable.
1.34.0 - 2024-11-01
- Fixed other packages that have a CVE like
Microsoft.Extensions.Caching.Memory
. Reported by @polajenko. Fixed by @linkdotnet.
1.33.3 - 2024-10-11
bunit.generators
respect parameters from the base class.- Supports components using constructor injection in
net9.0
.
- Use latest
System.Text.Json
due to CVE in8.0.4
.
1.32.7 - 2024-10-04
- Ensure that a check in
WaitForAssertion
and related methods is always performed at once before the wait timer is started. If not, the timeout could occure before a wait-for check-condition had been attempted, causing tests to fail without reason. Fixed by @egil.
1.31.3 - 2024-08-16
UploadFile
should only throw an exception when the file size exceeds the maximum allowed size. Reported by @candritzky. Fixed by @linkdotnet.
1.30.3 - 2024-07-21
UploadFile
doesn't throw an exception when the file size exceeds the maximum allowed size. Reported by @MorneZaayman in #1503. Fixed by @linkdotnet.
1.29.5 - 2024-07-05
- CI build changes to force running verification on x64 based AMD CPUs.
1.28.9 - 2024-04-19
- bUnit's built-in HTML parser did not correctly parse full HTML documents that included a <!DOCTYPE html> as the first element. Fixed by @egil.
@formname
directive led to anInvalidOperationException
when used on a form element. Reported by @suzu2469 in #1438. Fixed by @egil/@linkdotnet.
NavigationManager
is again registered as a singleton instead of scoped.
1.27.17 - 2024-03-02
- Support for
IKeyedServiceProvider
in net8.0. Reported by @ViRuSTriNiTy. By @linkdotnet. - Support for
net9.0
. NOTE, there is no commitment as of now to support net9.0 in bUnit v1. However. Support for net9.0 may move to a future v2 release of bUnit and be deprecated in v1. However, allowing bUnit to build and work with net9.0 previews allows our users to keep testing!
- Support for
SupplyFromQueryParameter
in net8.0. Reported by @aayjaychan. Fixed by @egil and @linkdotnet.
1.26.64 - 2023-12-20
- Upgraded AngleSharp to 1.0.7. Info: The usage of
AngleSharpWrappers
is not needed anymore. Any usage ofUnwrap
should not be needed anymore.
- When the
TestContext
was disposed, the Blazor Renderer itself didn't dispose components under test. By @linkdotnet. - When navigating, the
HistoryEntryState
onNavigationManager
will be populated. By @linkdotnet.
- New overloads for
IRenderedFragmentBase.InvokeAsync
that allow retrieving the work item's return value. By @jcparkyn.
1.25.3 - 2023-11-14
- Upgrade all .NET 8 preview dependencies to .NET 8 stable.
1.24.10 - 2023-10-14
- When the
TestContext
was disposed, it disposed of all services via the service provider. However, if there were ongoing renders happening, this could cause inconsistent state in the render tree, since theTestRenderer
could try to access the service provider to instantiate components.
This release changes the dispose phase such that the renderer gets disposed first, then the service provider. The disposal of any services that implementIAsyncDisposable
is now also awaited. Fixed by @egil and @linkdotnet. Reported by @BenSchoen in #1227.
-
Support for custom service provider factories (
IServiceProviderFactory<TContainerBuilder>
). This enables the use of Autofac and other frameworks for dependency injection like on real-world ASP.NET Core / Blazor projects. By @inf9144. -
Ability to raise the
oncancel
andonclose
event, that was introduced with .NET 8.
1.23.9 - 2023-09-06
- If the renderer was not idle when calling
SetParametersAndRender
, the method could return before the parameters were set and the component under test had finished rendering. This was a regression that happened in v1.21.9. Reported by @Skintkingle in #1188. Fixed by @egil.
net8.0
support- Increased timeout of
WaitForAssertion
to infinite when a debugger is attached. By @linkdotnet.
- AngleSharp IElement extension methods do not work with
IRenderedFragment.Find
. Reported by a2er. Fixed by @linkdotnet.
1.22.19 - 2023-07-28
- Update bunit templates to support the target framework version of the project. By @linkdotnet.
-
Calling
MarkupMatches(RenderFragment)
from a lambda passed to e.g.WaitForAssertion
could lead to a deadlock in certain circumstances. Fixed by @linkdotnet. Reported by @uecasm in #1143. -
Rendering complex component hierarchies could result in a stack overflow. Fixed by @egil.. Reported by @groogiam in #1064.
-
Remove capturing and dispatching markup updates to test frameworks synchronization context again. This could cause deadlocks and does not have any impact on test stability. Fixed by @egil. Reported by @biohazard999 in #1144.
1.21.9 - 2023-07-02
-
Allow using 3rd party
IComponentActivator
at the same time as component factories. By @egil. Reported by BenSchoen in #1129. -
Calling
IRenderedComponent.Render()
orIRenderedComponent.SetParametersAndRender()
did not batch up multiple synchronous re-renders after setting parameters. This is now changed such that the method causes the component to re-render with new parameters in the same way as if a parent component had passed new parameters to it. By @egil. Reported by @Jcparkyn in #1119.
1.20.8 - 2023-05-21
- Added static
DefaultWaitTimeout
property toTestContext
to enable overriding the default timeout of "wait for" methods likeWaitForAssertion
from 1 second to something else. By @egil.
- TestRenderer throws
ObjectDisposedException
if any methods is accessed after it has been disposed. It will also prevent changes to the internal render tree after it has been disposed. By @egil.
1.19.14 - 2023-04-26
- Custom elements with attributes throw
ArgumentException
withMarkupMatches
. Reported by @candritzky. Fixed by @linkdotnet.
- Changed test renderer such that updates to rendered components markup happen in the same synchronization context as the test framework is using (if any), if any, to avoid memory race conditions. By @egil.
1.18.4 - 2023-02-26
- Some characters where not properly escaped. Reported by @pwhe23. Fixed by @linkdotnet.
- Clicking a submit button or submit input element inside a form, submits the form, if the submit button or submit input element does not have the
@onclick:preventDefault
attribute set. Reported by @linkdotnet. Fixed by @egil.
1.17.2 - 2023-02-22
- Submit buttons and input fields now no longer cause a form submit when they have the
@onclick:preventDefault
attribute. By @JelleHissink.
1.16.2 - 2023-02-07
- Changed semantic comparer to handle elements parsed outside their proper context, e.g. an
<path>
element parsed without being inside a<svg>
element. The semantic comparer will now be able to treat those as regular elements and thus be able to compare correctly to other elements of the same type and with the same node name. By @egil.
1.15.5 - 2023-02-04
- Upgrade AngleSharp.Diffing to 0.17.1.
1.14.4 - 2023-01-11
- Added
IMemoryCache
by default to the Services container. By @linkdotnet.
- Added support in
FakeNavigationManager
to handle umlauts. - Fixed a bug where attribute values did not get escaped. Reported by @brettwinters. Fixed by @linkdotnet.
1.13.5 - 2022-12-16
This release contains a bunch of small tweaks and fixes.
1.12.6 - 2022-11-08
- The created HTML contained encoded strings. Reported by @tobiasbrandstaedter. Fixed by @linkdotnet.
1.11.7 - 2022-10-13
- Added the
StateFromJson
method to theNavigationHistory
type, to make it easy to deserialize navigation state stored as JSON during a call toNavigationManager.NavigateTo
, e.g. as seen with the newInteractiveRequestOptions
type available in .NET 7. By @linkdotnet and @egil.
1.10.14 - 2022-09-16
-
Added new test double
FakeWebAssemblyHostEnvironment
that implementsIWebAssemblyHostEnvironment
. By @KristofferStrube. -
Added
Bind
method to parameter builder that makes it easier to emulate the@bind-Value
syntax in C#-based tests.When writing tests in razor files, the `@bind-` directive can be directly applied like this:
<MyComponent @bind-Value="myParam"></MyComponent>
The same expression in C# syntax is more verbose like this:
RenderComponent<MyComponent>(ps => ps .Add(c => c.Value, value) .Add(c => c.ValueChanged, newValue => value = newValue) .Add(c => c.ValueExpression, () => value));
With the new `Bind` method this can be done in one method:
RenderComponent<MyComponent>(ps => ps .Bind(c => c.Value, value, newValue => value = newValue, () => value));
By [@linkdotnet](https://github.com/linkdotnet) and [@egil](https://github.com/egil).
-
Added support for
NavigationLock
, which allows user code to intercept and prevent navigation. By @linkdotnet and @egil.
JSInterop.VerifyInvoke
reported the wrong number of actual invocations of a given identifier. Reported by @otori. Fixed by @linkdotnet.
1.9.8 - 2022-06-07
WaitForAssertion
method is now marked as an assertion method with the[AssertionMethod]
attribute. This makes certain analyzers like SonarSource's Tests should include assertions happy. By @egil.
-
A race condition existed between
WaitForState
/WaitForAssertion
andFindComponents
, if the first used the latter. Reported by @rmihael, @SviatoslavK, and @RaphaelMarcouxCTRL. Fixed by @egil and @linkdotnet. -
Triggering of event handlers now runs entirely inside the renderers synchronization context, avoiding race condition between elements in the DOM tree being updated by the renderer and the event triggering logic traversing the DOM tree to find event handlers to trigger. Reported by @FlukeFan. Fixed by @egil.
1.8.15 - 2022-05-19
- Added test helpers that make it much easier to pass files to the
InputFile
component. Learn more in the documentation. By @egil and @linkdotnet.
Htmlizer
usesStringBuilder
instead ofList<string>
to reduce allocations and improve render speed. By @linkdotnet.
-
TestServiceProvider
now implementsIAsyncDisposable
. This meansTestContext.Dispose()
now calls the async disposable method as well as the non-async version on the service provider. It does however not block or await the task returned, since that can lead to deadlocks.To await the disposal of async services registered in the `TestContext.Services` container, do the following:
1.7.7 - 2022-04-29
-
Added method
SetAuthenticationType
toTestAuthorizationContext
to allow for custom authentication type checks. By @TimPurdum. -
Added
DisposeComponents
toTestContextBase
. It will dispose and remove all components rendered by theTestContextBase
. By @linkdotnet. -
Added .NET 7 as a target framework for bUnit. By @linkdotnet.
-
Fixed step by step guide for building and viewing the documentation locally. By @linkdotnet.
-
FakeNavigationManager.NavigateTo
could lead to exceptions when navigating to external url's. Reported by @TDroogers. Fixed by @linkdotnet.
1.6.4 - 2022-02-22
A quick minor release that primiarily fixes a regression in 1.5.12.
ClickAsync
could lead to bubbling exceptions fromGetDispatchEventTasks
even though they should be handled. Reported by @aguacongas. Fixed by @linkdotnet.- Added more non bubbling events to bUnit so it behaves closer to the HTML specification. @linkdotnet.
1.5.12 - 2022-02-15
This first release of 2022 includes one fix and four additions. A huge thank you to Steven Giesel (@linkdotnet) and Denis Ekart (@denisekart) for their contributions to this release.
Also a big shout out to bUnit's sponsors who helped make this release happen.
The higher tier sponsors are:
Other sponsors are:
- Hassan Rezk Habib (@hassanhabib)
- Jonny Larsson (@Garderoben)
- Domn Werner (@domn1995)
- Mladen Macanović (@stsrki)
- @ChristopheDEBOVE
- Steven Giesel (@linkdotnet)
- Added
FakeSignOutSessionStateManage
type in Blazor, that makes it easy to test components that use theSignOutSessionStateManage
type. By @linkdotnet. - Added a validation to
AddChildContent
method inComponentParameterCollectionBuilder
that will throw an exception if the component'sChildContent
is a generic type. By @denisekart. - Added more optional arguments for
Click
andDoubleClick
extensions which were introduced in .NET 5 and .NET 6. By @linkdotnet. - Added template support for
Nunit
andMSTest
unit test frameworks. By @denisekart.
- Changed
GetDispatchEventTasks
for bubbling events such that handled exceptions are not rethrown later from theWaitFor...
helpers methods. Reported by @AndrewStrickland. Fixed by @linkdotnet
1.4.15 - 2021-12-18
This release reintroduces Stub<TComponent>
and related back into the main library, so the "preview" library bunit.web.mock
is already obsolete.
A big shout out to bUnit's sponsors who helped make this release happen.
The higher tier sponsors are:
Other sponsors are:
- Hassan Rezk Habib (@hassanhabib)
- Jonny Larsson (@Garderoben)
- Domn Werner (@domn1995)
- Mladen Macanović (@stsrki)
- @ChristopheDEBOVE
- Add
ComponentFactories
extensions method that makes it easy to register an instance of a replacement component. By @egil. - Add ability to pass
ServiceProviderOptions
toTestServiceProvider
through property to allow users to customize the service provider. By @rodolfograve.
- Changed
SetParametersAndRender
such that it rethrows any exceptions thrown by the component under testsSetParametersAsync
method. Thanks to @bonsall for reporting the issue. Fixed by @egil. onclick
on a button inside a form will raise theonsubmit
event for the form itself. Reported by [@egil]. Fixed by @linkdotnet.- Only forms are allowed to have a
onsubmit
event handler. Whenonsubmit
is invoked from a non-form element results in an exception. Fixed by @linkdotnet.
1.3.42 - 2021-11-09
This release includes support for .NET 6, with support for all new features in Blazor with that release. There are also a number of additions and fixes, all listed below.
Big shout out to bUnit's sponsors who helped make this release happen.
The higher tier sponsors are:
Other sponsors are:
- Hassan Rezk Habib (@hassanhabib)
- Jonny Larsson (@Garderoben)
- Domn Werner (@domn1995)
- Mladen Macanović (@stsrki)
- @ChristopheDEBOVE
List of added functionality in this release.
-
Added support for writing tests of components that use the
<FocusOnNavigate>
component included in .NET 6. This includes an assertion helper methodVerifyFocusOnNavigateInvoke
on bUnit'sJSInterop
that allow you to verify that<FocusOnNavigate>
has set focus on an element during render. For example, to verify thath1
selector was used to pick an element to focus on, do:// <App /> component uses <FocusOnNavigate> var cut = RenderComponent<App>(); // Verifies that <FocusOnNavigate> called it's JavaScript function var invocation = JSInterop.VerifyFocusOnNavigateInvoke(); // Verify that the invocation of <FocusOnNavigate> JavaScript function included the "h1" as the selector Assert.Equal("h1", invocation.Arguments[0]);
By [@egil](https://github.com/egil).
-
Added fake version of the
PersistentComponentState
type in Blazor that makes it possible to test components that use the type. By @egil. -
Added
TriggerEvent
method to make it easier to trigger custom events. By @egil. -
Added
History
capture in theFakeNavigationManager
. By @egil. -
Added new bUnit component mocking library, available via NuGet as
bunit.web.mock
. It is currently in preview and the features/APIs of it will change! -
Added
WaitForElement
andWaitForElements
methods. These makes it possible to wait for one or more elements to appear in the DOM before continuing a test, similar to howWaitForAssertion
allows you to wait for an assertion to pass, orWaitForState
allows you to wait for a predicate to pass. By @egil.
- Added automatic conversion of values (types) passed to
Change()
andInput()
event trigger methods. This means that e.g. aDateTime
passed toChange()
is automatically converted to a string format that Blazor expects. By @egil.
-
The
Click
andDoubleClick
extension methods now set theMouseEventArgs.Detail
property to1
and2
respectively by default, unless the user specifies something else. This makes the methods more correctly emulate how Blazor reports single or double clicks on an element in the browser. Thanks to @David-Moreira for the help troubleshooting this issue. By @egil. -
FocusAsync()
method handler onElementReference
and<FocusOnNavigate>
js handler return completedTask
. By @anddrzejb. -
Fixes handling of disposed event handlers of bubbling events. See issue #518 for details. Thanks to @David-Moreira for helping debug this issue.
-
Async event trigger methods are not public. In most circumstances you do not need to use them, but if you have a scenario where you want to check that something has not happened after an event handler was triggered, then you can use the async methods and await them to know when they are completed. See #552 for details. By @egil.
1.2.49 - 2021-08-09
List of added functionality in this release.
-
Added more extensions methods to
MarkupMatchesAssertExtensions
to allow asserting withMarkupMatches
onIEnumerable
andIElement
. By @jgoday. -
Added
BunitErrorBoundaryLogger
implementation ofIErrorBoundaryLogger
(needed for Blazor's ErrorBoundary component in .NET 6.0). By @jgoday. -
Added
ComponentFactories
property to theTestContextBase
type. TheComponentFactories
property is aComponentFactoryCollection
type that containsIComponentFactory
types. These are used by bUnits component activator, whenever a component is created during testing. If no component factories is added to the collection, the standard component activator mechanism from Blazor is used. This feature makes it possible to control what components are created normally during a test, and which should be e.g. replaced by a test dummy. More info is available in issue #388. Learn more about this feature on the Controlling component instantiation page. -
Added
HasComponent<TComponent>()
toIRenderedFragement
. Use it to check if the rendered fragment contains a component of typeTComponent
. Added by @egil. -
Added
AddStub
andAdd
extension methods toComponentFactories
that makes it easy to configure bUnit to replace components in the render tree with stubs. Both methods have overloads that allow for fine grained selection of component types to "double" during testing. Added by @egil in #400.
List of changes in this release.
-
Updated AngleSharp and related libraries to 0.16.0. NOTE, the new version of AngleSharp includes nullable annotations, which might affect how your code compiles, if you have nullable checking enabled in your test project. By @egil.
-
Updated .NET 6 dependencies to preview 5. By @egil.
List of fixes in this release.
-
Fixed JSInterop error message when trying to import an unconfigured module. By @jgoday in #425.
-
Fixed issue where a registered fall-back service provider was not made available to resolve service dependencies of components under test. Thanks to @dady8889 for the reporting the issue.
-
Fixed handling of escaped uri's in FakeNavigationManager. By @linkdotnet in #460.
-
Captured error message from event dispatcher in renderer that would previously be hidden from the user. Related to issue #399.
1.1.5 - 2021-04-30
-
All bUnit assemblies is now strong named signed.
-
Added .NET 6 (preview 3) as a target framework for bUnit, bUnit.core and bUnit.web.
-
Changed bunit.template such that created projects only reference the bUnit package. Bumped other referenced packages to latest version.
-
Changed TestServiceProvider to validate scopes of registered services, such that it behaves like the service provider (default IoC container) in Blazor.
The following section list all changes since preview 02.
List of changes in existing functionality.
-
BREAKING CHANGE: Writing tests using the test components
<Fixture>
and<SnapshotTest>
components inside .razor files has been moved to its own library,bunit.web.testcomponents
. This was done for several reasons:-
The feature has been experimental since it was introduced, and it was introduced get a more natural way of specifying the component under test and any related markup used by test.
-
The feature is only supported with xUnit.
-
There are some issues related to the
SourceFileFinder
library, which is used to discover the test components. -
A better way of writing tests in .razor files has been added to bUnit, using "inline render fragments". This method works with all general purpose test frameworks, e.g. MSTest, NUnit, and xUnit, is more flexible, and offer less boilerplate code than the test components. The bUnit documentation has been updated with a guide to this style.
The new package `bunit.web.testcomponents` is provided as is, without expectation of further development or enhancements. If you are using the test components currently for writing tests, it will continue to work for you. If you are starting a new project, or have few of these tests, consider switching to the "inline render fragments" style. Here is a quick comparison of the styles, using a very simple component. First, the test component style:
@inherits TestComponentBase <Fixture Test="HelloWorldComponentRendersCorrectly"> <ComponentUnderTest> <HelloWorld /> </ComponentUnderTest> @code { void HelloWorldComponentRendersCorrectly(Fixture fixture) { // Act var cut = fixture.GetComponentUnderTest<HelloWorld>(); // Assert cut.MarkupMatches("<h1>Hello world from Blazor</h1>"); } } </Fixture> <SnapshotTest Description="HelloWorld component renders correctly"> <TestInput> <HelloWorld /> </TestInput> <ExpectedOutput> <h1>Hello world from Blazor</h1> </ExpectedOutput> </SnapshotTest>
The a single test in "inline render fragments" style covers both cases: @inherits TestContext @code { [Fact] public void HelloWorldComponentRendersCorrectly() { // Act var cut = Render(@<HelloWorld />); // Assert cut.MarkupMatches(@<h1>Hello world from Blazor</h1>); } } To make the snapshot test scenario even more compact, consider putting all code in one line, e.g. `Render(@<HelloWorld />).MarkupMatches(@<h1>Hello world from Blazor</h1>);`. For a more complete snapshot testing experience, I recommend looking at Simon Cropp's [Verify](https://github.com/VerifyTests) library, in particular the [Verify.Blazor extension to bUnit](https://github.com/VerifyTests/Verify.Blazor#verifybunit). Verify comes with all the features you expect from a snapshot testing library.
-
List of now removed features.
-
The
AddXunitLogger
method, which provided support for capturingILogger
messages and passing them to xUnit'sITestOutputHelper
, has been removed. There were no need to keep xUnit specific code around in bUnit going forward, and there are many implementations on-line that supports this feature, so having it in bUnit made little sense. One such alternative, which bUnit has adopted internally, is to use Serilog. This looks as follows:-
Add the following packages to your test project:
Serilog
,Serilog.Extensions.Logging
, andSerilog.Sinks.XUnit
. -
Add the following class/extension method to your test project (which replicates the signature of the removed
AddXunitLogger
method):using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Serilog; using Serilog.Events; using Xunit.Abstractions; namespace Bunit { public static class ServiceCollectionLoggingExtensions { public static IServiceCollection AddXunitLogger(this IServiceCollection services, ITestOutputHelper outputHelper) { var serilogLogger = new LoggerConfiguration() .MinimumLevel.Verbose() .WriteTo.TestOutput(outputHelper, LogEventLevel.Verbose) .CreateLogger(); services.AddSingleton<ILoggerFactory>(new LoggerFactory().AddSerilog(serilogLogger, dispose: true)); services.AddSingleton(typeof(ILogger<>), typeof(Logger<>)); return services; } } }
-
-
The
bunit.xunit
package has been removed, since it is no longer needed (there is no code left in it).
The following section list all changes in 1.0.0 preview 02.
The plan is to make this the last preview release of bUnit. If no big blocking bugs show up the next two weeks, a non-preview release of bUnit will be pushed out to the world.
List of new features.
-
Added the ability to pass a "fallback
IServiceProvider
" to theTestServiceProvider
, available through theServices
property on aTestContext
. The fallback service provider enables a few interesting scenarios, such as using an alternative IoC container, or automatically generating mocks of services components under test depend on. See the Injecting Services into Components Under Test page for more details on this feature. By @thopdev in #310. -
Added
Task<Expection> ITestRenderer.UnhandledException
property that returns aTask<Exception>
that completes when the renderer captures an unhandled exception from a component under test. If a component is missing exception handling of asynchronous operations, e.g. in theOnInitializedAsync
method, the exception will not break the test, because it happens on another thread. To have a test fail in this scenario, you can await theUnhandledException
property on theTestContext.Renderer
property, e.g.:using var ctx = new TestContext(); var cut = ctx.RenderComponent<ComponentThatThrowsDuringAsyncOperation>(); Task<Exception?> waitTimeout = Task.Delay(500).ContinueWith(_ => Task.FromResult<Exception?>(null)).Unwrap(); Exception? unhandledException = await Task.WhenAny<Exception?>(Renderer.UnhandledException, waitTimeout).Unwrap(); Assert.Null(unhandledException);
In this example, we await any unhandled exceptions from the renderer, or our wait timeout. The `waitTimeout` ensures that we will not wait forever, in case no unhandled exception is thrown. NOTE, a better approach is to use the `WaitForState` or `WaitForAssertion` methods, which now also throws unhandled exceptions. Using them, you do not need to set up a wait timeout explicitly. By [@egil](https://github.com/egil) in [#344](https://github.com/egil/bUnit/issues/344).
-
Added a simple fake navigation manager, which is registered by default in bUnit's service provider. When the fake navigation manager's
NavigateTo
method is called, it does two things:-
Set the
Uri
property to the URI passed to theNavigateTo
method (with the URI normalized to an absolute URI). -
Raise the
LocationChanged
event with the URI passed to theNavigateTo
method.Lets look at an example: To verify that the
<GoesToFooOnInit>
component below calls theNavigationManager.NavigateTo
method with the expected value, do the following:<GoesToFooOnInit>
component:@inject NavigationManager NavMan @code { protected override void OnInitialized() { NavMan.NavigateTo("foo"); } }
Test code:
// Arrange using var ctx = new TestContext(); var navMan = ctx.Services.GetRequiredService<NavigationManager>(); // Act var cut = ctx.RenderComponent<GoesToFooOnInit>(); // Assert Assert.Equal($"{navMan.BaseUri}foo", navMan.Uri);
Since the
foo
input argument is normalized to an absolute URI, we have to do the same normalization in our assertion.The fake navigation manager's
BaseUri
is set tohttp://localhost/
, but it is not recommended to use that URL directly in your code. Instead create an assertion by getting that value from theBaseUri
property, like shown in the example above.
-
-
Added additional bUnit JSInterop
Setup
methods, that makes it possible to get complete control of invocation matching for the created handler. By @egil.
List of changes in existing functionality.
-
WaitForAssertion
andWaitForState
now throws unhandled exception caught by the renderer from a component under test. This can happen if a component is awaiting an asynchronous operation that throws, e.g. a API call using a misconfiguredHttpClient
. By @egil in #310. -
Improvements to error message from bUnit's JSInterop when it receives an invocation that it has not been set up to handle. By @egil in #346.
List of now removed features.
List of any bug fixes.
The following section list all changes in 1.0.0 preview 01.
List of new features.
-
Added support for casting
BUnitJSRuntime
toIJSInProcessRuntime
andIJSUnmarshalledRuntime
. By @KristofferStrube in #279 -
Added support for triggering
@ontoggle
event handlers through a dedicatedToggle()
method. By @egil in #256. -
Added out of the box support for
<Virtualize>
component. When a<Virtualize>
component is used in a component under test, it's JavaScript interop-calls are faked by bUnits JSInterop, and it should result in all items being rendered immediately. By @egil in #240. -
Added support for components that call
ElementReference.FocusAsync
. These calls are handled by the bUnits JSInterop, that also allows you to verify thatFocusAsync
has been called for a specific element. For example, if a component has rendered an<input>
element, then the following code will verify that it has been focused usingFocusAsync
:var cut = RenderComponent<FocusingComponent>(); var input = cut.Find("input"); JSInterop.VerifyFocusAsyncInvoke() .Arguments[0] // the first argument is the ElemenetReference .ShouldBeElementReferenceTo(input);
By [@egil](https://github.com/egil) in [#260](https://github.com/egil/bUnit/pull/260).
-
Added
Render(RenderFragment)
andRender<TComponent>(RenderFragment)
methods toTestContext
, as well as various overloads to theMarkupMatches
methods, that also takes aRenderFragment
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):
@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:
@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).
-
Added support for
IJSRuntime.InvokeAsync<IJSObjectReference>(...)
calls from components. There is now a new setup helper methods for configuring how invocations towards JS modules should be handled. This is done with the variousSetupModule
methods available on theBunitJSInterop
type available through theTestContext.JSInterop
property. For example, to set up a module for handling calls tofoo.js
, do the following:using var ctx = new TestContext(); var moduleJsInterop = ctx.JSInterop.SetupModule("foo.js");
The returned `moduleJsInterop` is a `BunitJSInterop` type, which means all the normal `Setup<TResult>` and `SetupVoid` methods can be used to configure it to handle calls to the module from a component. For example, to configure a handler for a call to `hello` in the `foo.js` module, do the following:
moduleJsInterop.SetupVoid("hello");
By [@egil](https://github.com/egil) in [#288](https://github.com/egil/bUnit/pull/288).
-
Added support for registering services in bUnits
Services
collection that implementsIAsyncDisposable
. Suggested by @jmaillet in #249.
List of changes in existing functionality.
-
bUnit's mock IJSRuntime has been moved to an "always on" state by default, in strict mode, and is now available through
TestContext
'sJSInterop
property. This makes it possible for first party Blazor components like the<Virtualize>
component, which depend on JSInterop, to "just work" in tests.**Compatible with previous releases:** To get the same effect as calling `Services.AddMockJSRuntime()` in beta-11, which used to add the mock IJSRuntime in "loose" mode, you now just need to change the mode of the already on JSInterop, i.e. `ctx.JSInterop.Mode = JSRuntimeMode.Loose`. **Inspect registered handlers:** Since the new design allows registering invoke handlers in the context of the `TestContext`, you might need to get already registered handlers in your individual tests. This can be done with the `TryGetInvokeHandler()` method, that will return handler that can handle the parameters passed to it. E.g. to get a handler for a `IJSRuntime.InvokaAsync<string>("getValue")`, call `ctx.JSInterop.TryGetInvokeHandler<string>("getValue")`. Learn more [issue #237](https://github.com/egil/bUnit/issues/237). By [@egil](https://github.com/egil) in [#247](https://github.com/egil/bUnit/pull/247).
-
The
Setup<TResult>(string identifier, Func<IReadOnlyList<object?>, bool> argumentsMatcher)
andSetupVoid(string identifier, Func<IReadOnlyList<object?>, bool> argumentsMatcher)
methods in bUnits JSInterop/MockJSRuntime has a new second parameter, anInvocationMatcher
.The `InvocationMatcher` type is a delegate that receives a `JSRuntimeInvoation` and returns true. The `JSRuntimeInvoation` type contains the arguments of the invocation and the identifier for the invocation. This means old code using the `Setup` and `SetupVoid` methods should be updated to use the arguments list in `JSRuntimeInvoation`, e.g., change the following call: `ctx.JSInterop.Setup<string>("foo", args => args.Count == 2)` to this: `ctx.JSInterop.Setup<string>("foo", invocation => invocation.Arguments.Count == 2)`. Changed added in relation to [#240](https://github.com/egil/bUnit/issues/240) in [#257](https://github.com/egil/bUnit/issues/257) by [@egil](https://github.com/egil).
-
Changed
AddTestAuthorization
such that it works in Razor-based test contexts, i.e. on theFixture
andSnapshotTest
types.
List of now removed features.
- A few bUnit internal xUnit assert helper methods, the custom
ShouldAllBe
methods, has mistakingly been part of the bunit.xunit package. These have been removed.
List of any bug fixes.
-
When an
Add
call to the component parameter collection builder was used to select a parameter that was inherited from a base component, the builder incorrectly reported the selected property/parameter as missing on the type. Reported by @nickmuller in #250. -
When an element, found in the DOM tree using the
Find()
, method was removed because of an event handler trigger on it, e.g. ancut.Find("button").Click()
event trigger method, anElementNotFoundException
was thrown. Reported by @nickmuller in #251. -
In the built-in fake authentication system in bUnit, roles and claims were not available in components through the a cascading parameter of type
Task<AuthenticationState>
. Reported by @AFAde in #253 and fixed in #291 by @egil.
The following section list all changes in beta-11.
List of new features.
-
Two new overloads to the
RenderFragment()
andChildContent()
component parameter factory methods have been added that takes aRenderFragment
as input. By @egil in #203. -
Added a
ComponentParameterCollection
type. TheComponentParameterCollection
is a collection of component parameters, that knows how to turn those components parameters into aRenderFragment
, which will render a component and pass any parameters inside the collection to that component. That logic was spread out over multiple places in bUnit, and is now owned by theComponentParameterCollection
type. By @egil in #203. -
Added additional placeholder services for
NavigationManager
,HttpClient
, andIStringLocalizer
, to make it easier for users to figure out why a test is failing due to missing service registration before rendering a component. By @joro550 in #223. -
Added
Key
class that represents a keyboard key and helps to avoid constructingKeyboardEventArgs
object manually. The key can be passed toKeyPress
,KeyDown
, orKeyUp
helper methods to raise keyboard events. TheKey
class provides static special keys or can be obtained from character or string. Keys can be combined with key modifiers:Key.Enter + Key.Alt
.For example, this makes it easier to trigger keyboard events on an element:
var cut = ctx.RenderComponent<ComponentWithKeyboardEvents>(); var element = cut.Find("input"); element.KeyDown(Key.Enter + Key.Control); // Triggers onkeydown event with Ctrl + Enter element.KeyUp(Key.Control + Key.Shift + 'B'); // Triggers onkeyup event with Ctrl + Shift + B element.KeyPress('1'); // Triggers onkeypress event with key 1 element.KeyDown(Key.Alt + "<"); // Triggers onkeydown event with Alt + <
By [@duracellko](https://github.com/duracellko) in [#101](https://github.com/egil/bUnit/issues/101).
-
Added support for registering/adding components to a test context root render tree, which components under test is rendered inside. This allows you to simplify the "arrange" step of a test when a component under test requires a certain render tree as its parent, e.g. a cascading value.
For example, to pass a cascading string value `foo` to all components rendered with the test context, do the following:
ctx.RenderTree<CascadingValue<string>>(parameters => parameters.Add(p => p.Value, "foo")); var cut = ctx.RenderComponent<ComponentReceivingFoo>();
By [@egil](https://github.com/egil) in [#236](https://github.com/egil/bUnit/pull/236).
-
Added "catch-all"
Setup
method to bUnit's mock JS runtime, that allows you to specify only the type when setting up a planned invocation. By @nemesv in #234.
List of changes in existing functionality.
-
The
ComponentParameterBuilder
has been renamed toComponentParameterCollectionBuilder
, since it now builds theComponentParameterCollection
type, introduced in this release of bUnit. By @egil in #203. -
ComponentParameterCollectionBuilder
now allows adding cascading values that is not directly used by the component type it targets. This makes it possible to add cascading values to children of the target component. By @egil in #203. -
The
Add(object)
has been replaced byAddCascadingValue(object)
inComponentParameterCollectionBuilder
, to make it more clear that an unnamed cascading value is being passed to the target component or one of its child components. It is also possible to pass unnamed cascading values using theAdd(parameterSelector, value)
method, which now correctly detect if the selected cascading value parameter is named or unnamed. By @egil in #203. -
It is now possible to call the
Add()
,AddChildContent()
methods onComponentParameterCollectionBuilder
, and the factory methodsRenderFragment()
,ChildContent()
, andTemplate()
, multiple times for the same parameter, if it is of typeRenderFragment
orRenderFragment<TValue>
. Doing so previously would either result in an exception or just the last passedRenderFragment
to be used. Now all the providedRenderFragment
orRenderFragment<TValue>
will be combined at runtime into a singleRenderFragment
orRenderFragment<TValue>
.For example, this makes it easier to pass e.g. both a markup string and a component to a `ChildContent` parameter:
var cut = ctx.RenderComponent<Component>(parameters => parameters .AddChildContent("<h1>Below you will find a most interesting alert!</h1>") .AddChildContent<Alert>(childParams => childParams .Add(p => p.Heading, "Alert heading") .Add(p => p.Type, AlertType.Warning) .AddChildContent("<p>Hello World</p>") ) );
By [@egil](https://github.com/egil) in [#203](https://github.com/egil/bUnit/pull/203).
-
All test doubles are now in the same namespace,
Bunit.TestDoubles
. So all import statements forBunit.TestDoubles.JSInterop
andBunit.TestDoubles.Authorization
must be changed toBunit.TestDoubles
. By @egil in #223. -
Marked MarkupMatches methods as assertion methods to stop SonarSource analyzers complaining about missing assertions in tests. By @egil in #229.
-
AddTestAuthorization
now extendsTestContext
instead ofTestServiceProvider
, and also automatically adds theCascadingAuthenticationState
component to the root render tree. @egil in #237.
List of now removed features.
- The async event dispatcher helper methods have been removed (e.g.
ClickAsync()
), as they do not provide any benefit. If you have an event that triggers async operations in the component under test, instead usecut.WaitForState()
orcut.WaitForAssertion()
to await the expected state in the component.
List of any bug fixes.
- Using the ComponentParameterCollectionBuilder's
Add(p => p.Param, value)
method to add a unnamed cascading value didn't create an unnnamed cascading value parameter. By @egil in #203. Credits to Ben Sampica (@benjaminsampica) for reporting and helping investigate this issue. - Triggered events now bubble correctly up the DOM tree and triggers other events of the same type. This is a potentially breaking change, since this changes the behaviour of event triggering and thus you might see tests start breaking as a result hereof. By @egil in #119.
The following section list all changes in beta-10.
List of new features.
- Added support for .NET 5 RC-1.
List of changes in existing functionality.
- Related to #189, a bunch of the core
ITestRenderer
and related types have changed. The internals ofITestRenderer
is now less exposed and the test renderer is now in control of when rendered components and rendered fragments are created, and when they are updated. This enables the test renderer to protect against race conditions when theFindComponent
,FindComponents
,RenderFragment
, andRenderComponent
methods are called.
List of any bug fixes.
- Fixes #189: The test renderer did not correctly protect against a race condition during initial rendering of a component, and that could in some rare circumstances cause a test to fail when it should not. This has been addressed in this release with a major rewrite of the test renderer, which now controls and owns the rendered component and rendered fragment instances which is created when a component is rendered. By @egil in #201. Credits to @Smurf-IV for reporting and helping investigate this issue.
This release contains a couple of fixes, and adds support for .NET Preview 8 and later. There are no breaking changes in this release.
Thanks to pharry22 for submitting fixes and improvements to the documentation.
List of new features.
- Added
InvokeAsync(Func<Task>)
toRenderedComponentInvokeAsyncExtensions
. By @JeroenBos in #151. - Added
ITestRenderer Renderer { get ; }
toIRenderedFragment
to make it possible to simplify theIRenderedComponentBase<TComponent>
interface. By @JeroenBos in #151. - Added support for scoped CSS to
MarkupMatches
and related comparer methods. By @egil in #195.
List of changes in existing functionality.
- Moved
InvokeAsync()
,Render()
andSetParametersAndRender()
methods out ofIRenderedComponentBase<TComponent>
into extension methods. By @JeroenBos in #151. - Accessing
Markup
,Nodes
and related methods on a rendered fragment whose underlying component has been removed from the render tree (disposed) now throws aComponentDisposedException
. By @egil in #184. - Changed bUnit's build to target both .net 5.0 and .net standard 2.1. By @egil in #187.
List of any bug fixes.
- Fixes #175: When a component referenced in a test, e.g. through the
FindComponent()
method was removed from the render tree, accessing the reference could caused bUnit to look for updates to it in the renderer, causing a exception to be thrown. By @egil in #184.
Here is beta-8, a small summer vacation release this time. A few needed additions, especially around testing components that use Blazor's authentication and authorization. In addition to this, a lot of documentation has been added to https://bunit.egilhansen.com/docs/getting-started/.
List of new features.
-
Authorization fakes added to make it much easier to test components that use authentication and authorization. Learn more in the Faking Blazor's Authentication and Authorization page. By @DarthPedro in #151.
-
Added
MarkupMatches(this string actual ...)
extension methods. Make it easier to compare just the text content from a DON text node with a string, while still getting the benefit of the semantic HTML comparer.
List of changes in existing functionality.
TestContextBase.Dispose
made virtual to allow inheritor's to override it. By @SimonCropp in #137.- [Breaking change] Changed naming convention for JSMock feature and moved to new namespace,
Bunit.TestDoubles.JSInterop
. All classes and methods containingJs
(meaning JavaScript) renamed toJS
for consistency with Blazor'sIJSRuntime
. By @yourilima in #150
There are three big changes in bUnit in this release, as well as a whole host of small new features, improvements to the API, and bug fixes. The three big changes are:
- A splitting of the library
- Discovery of razor base tests, and
- A strongly typed way to pass parameters to a component under test.
There are also some breaking changes, which we will cover first.
NOTE: The documentation is next on the TODO list, so please bear with me while I update it to reflect all the recent changes.
Due to the big restructuring of the library, there are some breaking changes, hopefully for the better.
Previously, the Test
and Setup
methods on <Fixture>
and <SnapshotTest>
did not have any arguments, and the test context they represented when running, was implicitly available in the scope. This has changed with this release, such that all Test
and Setup
methods now receive the text context as an argument, and that should be used to call e.g. GetComponentUnderTest()
on.
For example, if you have a razor based test that looks like this currently:
<Fixture Test="Test001" Setup="TestSetup">
<ComponentUnderTest><Counter /></ComponentUnderTest>
<Fragment>...</Fragment>
</Fixture>
@code {
void TestSetup() => Services.AddMockJsRuntime();
void Test001()
{
var cut = GetComponentUnderTest<Counter>();
var fragment = GetFragment();
}
}
You have to change it to this:
<Fixture Test="Test001" Setup="TestSetup">
<ComponentUnderTest><Counter /></ComponentUnderTest>
</Fixture>
@code {
// Add a Fixture fixture argument to the setup method and use
// the services collection inside the fixture to register dependencies
void TestSetup(Fixture fixture) => fixture.Services.AddMockJsRuntime();
// Add a Fixture fixture argument to the test method
void Test001(Fixture fixture)
{
// Use the fixture instance to get the component under test
var cut = fixture.GetComponentUnderTest<Counter>();
var fragment = fixture.GetFragment();
}
}
It is a little more typing, but it is also a lot more obvious what is going on, e.g. where the component under test or fragment is coming from.
In addition to this, the Tests
and TestsAsync
methods on <Fixture>
have been deprecated in this release and throws a runtime exception if used. They were not very used and caused confusion about the state of the components under test between the method calls. Now you can only specify either a Test
or TestAsync
method per <Fixture>
.
The WaitForRender
method has been removed entirely from the library. Since it would only wait for one render, it had a very specific use case, where as the more general WaitForAssertion
or WaitForState
will wait for any number of renders, until the assertion passes, or the state predicate returns true. These make them much better suited to create stable tests.
With WaitForRender
, you would pass in an action that would cause a render before attempting your assertion, e.g.:
cut.WaitForRender(() => mockForecastService.Task.SetResult(forecasts));
Assert.Equal("...", cut.Markup);
This can now be changed to first call the action that will trigger the render, and then wait for an assertion to pass, using WaitForAssertion
:
mockForecastService.Task.SetResult(forecasts);
cut.WaitForAssertion(() => Assert.Equal("...", cut.Markup));
The two "wait for" methods are also only available through a rendered fragment or rendered component now.
Previously, the recommended method for creating xUnit component test classes was to inherit from ComponentTestFixture
. Due to the restructuring of the library, this type is now just a TestContext
with static component parameters factory methods, so it does not add much value anymore.
The component parameter factory methods are now also available in the more general purpose ComponentParameterFactory
type, which can be imported into all test classes, not just xUnit ones, using the import static Bunit.ComponentParameterFactory
method, and then you can change your existing xUnit test classes to inherit from TestContext
instead of ComponentTestFixture
to keep the current functionality for xUnit test classes.
That covers the most important breaking changes. Now lets look at the other big changes.
In this release sees bUnit refactored and split up into three different sub libraries. The reasons for doing this are:
- To make it possible to extract the direct dependency on xUnit and easily add support for NUnit or MSTest
- To make it easier to maintain distinct parts of the library going forward
- To enable future support for other non-web variants of Blazor, e.g. the Blazor Mobile Bindings.
The three parts of the library is now:
- bUnit.core: The core library only contains code related to the general Blazor component model, i.e. it is not specific to the web version of Blazor.
- bUnit.web: The web library, which has a dependency on core, provides all the specific types for rendering and testing Blazor web components.
- bUnit.xUnit: The xUnit library, which has a dependency on core, has xUnit specific extensions to bUnit, that enable logging to the test output through the
ILogger
interface in .net core, and an extension to xUnit's test runners, that enable it to discover and run razor based tests defined in.razor
files.
To keep things compatible with previous releases, an additional package is available, bUnit, which includes all of three libraries. That means existing users should be able to keep their single <PackageReference Include="bunit">
in their projects.
One of the pain points of writing Razor based tests in .razor
files was that the individual tests was not correctly discovered. That meant that if had multiple tests in a file, you would not see them in Visual Studios Test Explorer individually, you could not run them individually, and error was not reported individually.
This has changed with the bUnit.xUnit library, that now includes a way for it to discover individual razor tests, currently either a <Fixture>
or <SnapshotTest>
inside test components defined in .razor
files. It also enables you to navigate to the test by double clicking on it in the Test Explorer, and you can run each test individually, and see error reports individually.
WARNING: You still have to wait for the Blazor compiler to translate the .razor
files into .cs
files, before the tests show up in the Test Explorer, and the this can trip up the Test Explorer. So while this feature is a big improvement to razor based testing, it is still not perfect, and more works need to be done to refine it.
If you prefer writing your tests in C# only, you will be happy to know that there is now a new strongly typed way to pass parameters to components, using a builder. E.g., to render a ContactInfo
component:
var cut = RenderComponent<ContactInfo>(parameters => parameters
.Add(p => p.Name, "Egil Hansen")
.Add(p => p.Country, "Iceland")
);
There are a bunch of different Add
methods available on the builder, that allows you to easily pass in a EventCallback
, ChildContent
, or RenderFragment
.
The old way using the component parameter factory methods are still available if you prefer that syntax.
NOTE: The parameter builder API is experimental at this point, and will likely change.
The latest version of the library is available on NuGet in various incarnations:
Thanks to Martin Stühmer (@samtrion) and Stef Heyenrath (@StefH) for their code contributions in this release, and to Brad Wilson (@bradwilson) for his help with enabling xUnit to discover and run Razor based tests.
Also a big thank to all you who have contributed by raising issues, participated in issues by helping answer questions and providing input on design and technical issues.
- A new event,
OnAfterRender
, has been added toIRenderedFragmentBase
, whichIRenderedFragment
inherits from. Subscribers will be invoked each time the rendered fragment is re-rendered. Related issue #118. - A new property,
RenderCount
, has been added toIRenderedFragmentBase
, whichIRenderedFragment
inherits from. Its represents the number of times a rendered fragment has been rendered. Related issue #118. - A new event,
OnMarkupUpdated
, has been added toIRenderedFragmentBase
. Subscribers will be notifid each time the rendered fragments markup has been regenerated. Related issue #118. - Due to the concurrency bug discovered, the entire render notification and markup notification system has been changed.
- A new overload
RenderComponent()
andSetParameterAndRender()
, which takes aAction<ComponentParameterBuilder<TComponent>>
as input. That allows you to pass parameters to a component under test in a strongly typed way. Thanks to @StefH for the work on this. Related issues: #79 and #36. - The two razor test types,
<Fixture>
and<SnapshotTest>
, can now be skipped. by setting theSkip="some reason for skipping"
parameter. Note, this requires support from the test runner, which current only includes bUnit.xUnit. Related issue: #77. - The two razor test types,
<Fixture>
and<SnapshotTest>
, can now have a timeout specified, by setting theTimeout="TimeSpan.FromSeconds(2)"
parameter. Note, this requires support from the test runner, which current only includes bUnit.xUnit. - An
InvokeAsync
method has been added to theIRenderedFragmentBase
type, which allows invoking of an action in the context of the associated renderer. Related issue: #82. - Enabled the "navigate to test" in Test Explorer. Related issue: #106.
- Enabled xUnit to discover and run Razor-based tests. Thanks to Brad Wilson (@bradwilson) for his help with this. Related issue: #4.
- Better error description from
MarkupMatches
when two sets of markup are different. - The
JsRuntimePlannedInvocation
can now has its response to an invocation set both before and after an invocation is received. It can also have a new response set at any time, which will be used for new invocations. Related issue: #78. - The
IDiff
assertion helpers likeShouldHaveChanges
now takes anIEnumerable<IDiff>
as input to make it easier to call in scenarios where only an enumerable is available. Related issue: #87. TextContext
now registers all its test dependencies as services in theServices
collection. This now includes theHtmlParser
andHtmlComparer
. Related issue: #114.
- The
ComponentTestFixture
has been deprecated in this release, since it just inherits fromTestContex
and surface the component parameter factory methods. Going forward, users are encouraged to instead inherit directly fromTestContext
in their xUnit tests classes, and add aimport static Bunit.ComponentParameterFactory
to your test classes, to continue to use the component parameter factory methods. Related issue: #108.
<Fixture>
tests no longer supports splitting the test method/assertion step into multiple methods through theTests
andTestsAsync
parameters.WaitForRender
has been removed entirely from the library, as the more general purposeWaitForAssertion
orWaitForState
covers its use case.WaitForAssertion
orWaitForState
is no longer available onITestContext
types. They are still available on rendered components and rendered fragments.CreateNodes
method has been removed fromITextContext
. The ability to convert a markup string to aINodeList
is available through theHtmlParser
type registered inITextContext.Services
service provider.RenderEvents
has been removed fromIRenderedFragment
, and replaced by theOnMarkupUpdated
andOnAfterRender
events. Related issue #118.- The generic collection assertion methods
ShouldAllBe<T>(this IEnumerable<T> collection, params Action<T, int>[] elementInspectors)
andShouldAllBe<T>(this IEnumerable<T> collection, params Action<T>[] elementInspectors)
have been removed from the library.
- A concurrency issue would surface when a component under test caused asynchronous renders that was awaited using the
WaitForRender
,WaitForState
, orWaitForAssertion
methods. Related issue #118. MarkupMatches
and the related semantic markup diffing, didn't correctly ignore the__internal_stopPropagation_
and__internal_preventDefault_
added by Blazor to the rendered markup, when users use the:stopPropagation
and:preventDefault
modifiers. Thanks to @samtrion for reporting and solving this. Related issue: #111.cut.FindComponent<TComponent>()
didn't return the component inside the component under test. It now searches and finds the first child component of the specified type.
This release includes a name change from Blazor Components Testing Library to bUnit. It also brings along two extra helper methods for working with asynchronously rendering components during testing, and a bunch of internal optimizations and tweaks to the code.
Why change the name? Naming is hard, and I initial chose a very product-namy name, that quite clearly stated what the library was for. However, the name isn't very searchable, since it just contains generic keywords, plus, bUnit is just much cooler. It also gave me the opportunity to remove my name from all the namespaces and simplify those.
Hugh thanks to Rastislav Novotný (@duracellko)) for his input and review of the WaitForX
logic added in this release.
The latest version of the library is availble on NuGet:
Type | Link | |
---|---|---|
Library | https://www.nuget.org/packages/bunit/ | |
Template | https://www.nuget.org/packages/bunit.template/ |
-
WaitForState(Func<bool> statePredicate, TimeSpan? timeout = 1 second)
has been added toITestContext
andIRenderedFragment
. This method will wait (block) until the provided statePredicate returns true, or the timeout is reached (during debugging the timeout is disabled). Each time the renderer in the test context renders, or the rendered fragment renders, the statePredicate is evaluated.You use this method, if you have a component under test, that requires _one or more asynchronous triggered renders_, to get to a desired state, before the test can continue. The following example tests the `DelayedRenderOnClick.razor` component:
// DelayedRenderOnClick.razor <p>Times Clicked: @TimesClicked</p> <button @onclick="ClickCounter">Trigger Render</button> @code { public int TimesClicked { get; private set; } async Task ClickCounter() { await Task.Delay(1); // wait 1 millisecond TimesClicked += 1; } }
This is a test that uses `WaitForState` to wait until the component under test has a desired state, before the test continues:
[Fact] public void WaitForStateExample() { // Arrange var cut = RenderComponent<DelayedRenderOnClick>(); // Act cut.Find("button").Click(); cut.WaitForState(() => cut.Instance.TimesClicked == 1); // Assert cut.Find("p").TextContent.ShouldBe("Times Clicked: 1"); }
-
WaitForAssertion(Action assertion, TimeSpan? timeout = 1 second)
has been added toITestContext
andIRenderedFragment
. This method will wait (block) until the provided assertion method passes, i.e. runs without throwing an assert exception, or until the timeout is reached (during debugging the timeout is disabled). Each time the renderer in the test context renders, or the rendered fragment renders, the assertion is attempted.You use this method, if you have a component under test, that requires _one or more asynchronous triggered renders_, to get to a desired state, before the test can continue. This is a test that tests the `DelayedRenderOnClick.razor` listed above, and that uses `WaitForAssertion` to attempt the assertion each time the component under test renders:
[Fact] public void WaitForAssertionExample() { // Arrange var cut = RenderComponent<DelayedRenderOnClick>(); // Act cut.Find("button").Click(); // Assert cut.WaitForAssertion( () => cut.Find("p").TextContent.ShouldBe("Times Clicked: 1") ); }
-
Added support for capturing log statements from the renderer and components under test into the test output. To enable this, add a constructor to your test classes that takes the
ITestOutputHelper
as input, then in the constructor callServices.AddXunitLogger
and pass theITestOutputHelper
to it, e.g.:// ComponentTest.cs public class ComponentTest : ComponentTestFixture { public ComponentTest(ITestOutputHelper output) { Services.AddXunitLogger(output, minimumLogLevel: LogLevel.Debug); } [Fact] public void Test1() ... }
For Razor and Snapshot tests, the logger can be added almost the same way. The big difference is that it must be added during _Setup_, e.g.:
// RazorComponentTest.razor <Fixture Setup="Setup" ...> ... </Fixture> @code { private ITestOutputHelper _output; public RazorComponentTest(ITestOutputHelper output) { _output = output; } void Setup() { Services.AddXunitLogger(_output, minimumLogLevel: LogLevel.Debug); } }
-
Added simpler
Template
helper method To make it easier to test components withRenderFragment<T>
parameters (template components) in C# based tests, a newTemplate<TValue>(string name, Func<TValue, string> markupFactory)
helper methods have been added. It allows you to create a mock template that uses themarkupFactory
to create the rendered markup from the template.This is an example of testing the `SimpleWithTemplate.razor`, which looks like this:
@typeparam T @foreach (var d in Data) { @Template(d); } @code { [Parameter] public RenderFragment<T> Template { get; set; } [Parameter] public IReadOnlyList<T> Data { get; set; } = Array.Empty<T>(); }
And the test code:
var cut = RenderComponent<SimpleWithTemplate<int>>( ("Data", new int[] { 1, 2 }), Template<int>("Template", num => $"<p>{num}</p>") ); cut.MarkupMatches("<p>1</p><p>2</p>");
Using the more general `Template` helper methods, you need to write the `RenderTreeBuilder` logic yourself, e.g.:
var cut = RenderComponent<SimpleWithTemplate<int>>( ("Data", new int[] { 1, 2 }), Template<int>("Template", num => builder => builder.AddMarkupContent(0, $"<p>{num}</p>")) );
-
Added logging to TestRenderer. To make it easier to understand the rendering life-cycle during a test, the
TestRenderer
will now log when ever it dispatches an event or renders a component (the log statements can be access by capturing debug logs in the test results, as mentioned above). -
Added some of the Blazor frameworks end-2-end tests. To get better test coverage of the many rendering scenarios supported by Blazor, the ComponentRenderingTest.cs tests from the Blazor frameworks test suite has been converted from a Selenium to a bUnit. The testing style is very similar, so few changes was necessary to port the tests. The two test classes are here, if you want to compare:
-
Namespaces is now
Bunit
The namespaces have changed fromEgil.RazorComponents.Testing.Library.*
to simplyBunit
for the library, andBunit.Mocking.JSInterop
for the JSInterop mocking support. -
Auto-refreshing
IElement
s returned fromFind()
IRenderedFragment.Find(string cssSelector)
now returns aIElement
, which internally will refresh itself, whenever the rendered fragment it was found in, changes. This means you can now search for an element once in your test and assign it to a variable, and then continue to assert against the same instance, even after triggering renders of the component under test.For example, instead of having `cut.Find("p")` in multiple places in the same test, you can do `var p = cut.Find("p")` once, and the use the variable `p` all the places you would otherwise have the `Find(...)` statement.
-
Refreshable element collection returned from
FindAll
. TheFindAll
query method onIRenderedFragment
now returns a new type, theIRefreshableElementCollection<IElement>
type, and the method also takes a second optional argument now,bool enableAutoRefresh = false
.The `IRefreshableElementCollection` is a special collection type that can rerun the query to refresh its the collection of elements that are found by the CSS selector. This can either be done manually by calling the `Refresh()` method, or automatically whenever the rendered fragment renders and has changes, by setting the property `EnableAutoRefresh` to `true` (default set to `false`). Here are two example tests, that both test the following `ClickAddsLi.razor` component:
<ul> @foreach (var x in Enumerable.Range(0, Counter)) { <li>@x</li> } </ul> <button @onclick="() => Counter++"></button> @code { public int Counter { get; set; } = 0; }
The first tests uses auto refresh, set through the optional parameter `enableAutoRefresh` passed to FindAll:
public void AutoRefreshQueriesForNewElementsAutomatically() { var cut = RenderComponent<ClickAddsLi>(); var liElements = cut.FindAll("li", enableAutoRefresh: true); liElements.Count.ShouldBe(0); cut.Find("button").Click(); liElements.Count.ShouldBe(1); }
The second test refreshes the collection manually through the `Refresh()` method on the collection:
public void RefreshQueriesForNewElements() { var cut = RenderComponent<ClickAddsLi>(); var liElements = cut.FindAll("li"); liElements.Count.ShouldBe(0); cut.Find("button").Click(); liElements.Refresh(); // Refresh the collection liElements.Count.ShouldBe(1); }
-
Custom exception when event handler is missing. Attempting to triggering a event handler on an element which does not have an handler attached now throws a
MissingEventHandlerException
exception, instead of anArgumentException
.
WaitForNextRender
has been deprecated (marked as obsolete), since the addedWaitForState
andWaitForAssertion
provide a much better foundation to build stable tests on. The plan is to remove completely from the library with the final 1.0.0 release.
-
AddMockHttp
and related helper methods have been removed. The mocking of HTTPClient, supported through the mockhttp library, has been removed from the library. This was done because the library really shouldn't have a dependency on a 3. party mocking library. It adds maintenance overhead and uneeded dependencies to it.If you are using mockhttp, you can easily add again to your testing project. See [TODO Guide to mocking HttpClient](#) in the docs to learn how.
- Wrong casing on keyboard event dispatch helpers.
The helper methods for the keyboard events was not probably cased, so that has been updated. E.g. from
Keypress(...)
toKeyPress(...)
.