Table of Contents

Class BunitContext

Namespace
Bunit
Assembly
bunit.dll

A test context is a factory that makes it possible to create components under tests.

public class BunitContext : IDisposable, IAsyncDisposable
Inheritance
BunitContext
Implements
Derived
Inherited Members
Extension Methods

Constructors

BunitContext()

Initializes a new instance of the BunitContext class.

public BunitContext()

Properties

ComponentFactories

Gets the ComponentFactoryCollection. Factories added to it will be used to create components during testing, starting with the last added factory. If no factories in the collection can create a requested component, then the default Blazor factory is used.

public ComponentFactoryCollection ComponentFactories { get; }

Property Value

ComponentFactoryCollection

DefaultWaitTimeout

Gets or sets the default wait timeout used by "WaitFor" operations, i.e. WaitForAssertion<TComponent>(IRenderedComponent<TComponent>, Action, TimeSpan?), and JSInterop invocation handlers that have not been configured with results.

public static TimeSpan DefaultWaitTimeout { get; set; }

Property Value

TimeSpan

Remarks

The default is 1 second.

JSInterop

Gets bUnits JSInterop, that allows setting up handlers for InvokeAsync<TValue>(string, object[]) invocations that components under tests will issue during testing. It also makes it possible to verify that the invocations has happened as expected.

public BunitJSInterop JSInterop { get; }

Property Value

BunitJSInterop

RenderTree

Gets the RootRenderTree that all components rendered with the Render<TComponent>() methods, are rendered inside.

public RootRenderTree RenderTree { get; }

Property Value

RootRenderTree

Remarks

Use this to add default layout- or root-components which a component under test should be rendered under.

Renderer

Gets the renderer used by the test context.

public BunitRenderer Renderer { get; }

Property Value

BunitRenderer

Services

Gets the service collection and service provider that is used when a component is rendered by the test context.

public BunitServiceProvider Services { get; }

Property Value

BunitServiceProvider

Methods

AddAuthorization()

Adds the appropriate Blazor authentication and authorization services to the BunitServiceProvider to enable an authenticated user, as well as adding the CascadingAuthenticationState component to the test contexts render tree.

public BunitAuthorizationContext AddAuthorization()

Returns

BunitAuthorizationContext

AddBunitPersistentComponentState()

Adds and returns a BunitPersistentComponentState to the services of this BunitContext.

public BunitPersistentComponentState AddBunitPersistentComponentState()

Returns

BunitPersistentComponentState

The added BunitPersistentComponentState.

BuildRenderTree(RenderTreeBuilder)

Dummy method required to allow Blazor's compiler to generate C# from .razor files.

protected virtual void BuildRenderTree(RenderTreeBuilder builder)

Parameters

builder RenderTreeBuilder

Dispose()

public void Dispose()

Dispose(bool)

Disposes of the test context resources, in particular it disposes the Services service provider.

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

Set to true if called from Dispose(), false if called from a finalizer.f.

Remarks

The disposing parameter should be false when called from a finalizer, and true when called from the Dispose() method. In other words, it is true when deterministically called and false when non-deterministically called.

DisposeAsync()

public ValueTask DisposeAsync()

Returns

ValueTask

DisposeAsyncCore()

Disposes of the test context resources that are asynchronous, in particular it disposes the Services service provider.s

protected virtual ValueTask DisposeAsyncCore()

Returns

ValueTask

DisposeComponentsAsync()

Disposes all components rendered via this BunitContext.

public Task DisposeComponentsAsync()

Returns

Task

Render(RenderFragment)

Renders the renderFragment and returns it as a IRenderedComponent<TComponent>.

public virtual IRenderedComponent<ContainerFragment> Render(RenderFragment renderFragment)

Parameters

renderFragment RenderFragment

The render fragment to render.

Returns

IRenderedComponent<ContainerFragment>

The IRenderedComponent<TComponent>.

RenderComponent<TComponent>()

[Obsolete("Use Render instead.", true, UrlFormat = "https://bunit.dev/docs/migrations")]
public IRenderedComponent<TComponent> RenderComponent<TComponent>() where TComponent : IComponent

Returns

IRenderedComponent<TComponent>

Type Parameters

TComponent

RenderComponent<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>?)

[Obsolete("Use Render instead.", true, UrlFormat = "https://bunit.dev/docs/migrations")]
public IRenderedComponent<TComponent> RenderComponent<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>? parameterBuilder) where TComponent : IComponent

Parameters

parameterBuilder Action<ComponentParameterCollectionBuilder<TComponent>>

Returns

IRenderedComponent<TComponent>

Type Parameters

TComponent

Render<TComponent>(RenderFragment)

Renders the renderFragment and returns the first TComponent in the resulting render tree.

public virtual IRenderedComponent<TComponent> Render<TComponent>(RenderFragment renderFragment) where TComponent : IComponent

Parameters

renderFragment RenderFragment

The render fragment to render.

Returns

IRenderedComponent<TComponent>

The RenderedComponent<TComponent>.

Type Parameters

TComponent

The type of component to find in the render tree.

Remarks

Calling this method is equivalent to calling Render(renderFragment).FindComponent<TComponent>().

Render<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>?)

Instantiates and performs a first render of a component of type TComponent.

public virtual IRenderedComponent<TComponent> Render<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>>? parameterBuilder = null) where TComponent : IComponent

Parameters

parameterBuilder Action<ComponentParameterCollectionBuilder<TComponent>>

The ComponentParameterBuilder action to add type safe parameters to pass to the component when it is rendered.

Returns

IRenderedComponent<TComponent>

The rendered TComponent.

Type Parameters

TComponent

Type of the component to render.

SetRendererInfo(RendererInfo?)

Sets the RendererInfo for the renderer.

public void SetRendererInfo(RendererInfo? rendererInfo)

Parameters

rendererInfo RendererInfo
.NET Foundation

Supported by the .NET Foundation.