Table of Contents

Class TestContextWrapper

Namespace
Bunit
Assembly
Bunit.Web.dll

Represents a class that wraps a TestContext. Useful for simplifying writing tests in NUnit and MSTest, which cannot inherit directly from TestContext.

public abstract class TestContextWrapper
Inheritance
TestContextWrapper
Inherited Members
Extension Methods

Constructors

TestContextWrapper()

protected TestContextWrapper()

Properties

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 service collection and service provider that is used when a component is rendered by the test context.

public RootRenderTree RenderTree { get; }

Property Value

RootRenderTree

Renderer

Gets the renderer used by the test context.

public ITestRenderer Renderer { get; }

Property Value

ITestRenderer

Services

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

public TestServiceProvider Services { get; }

Property Value

TestServiceProvider

TestContext

Gets an instance of the wrapped TestContext, if any.

protected virtual TestContext? TestContext { get; set; }

Property Value

TestContext

Methods

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

DisposeComponents()

Disposes all components rendered via this TestContext.

public virtual void DisposeComponents()

Render(RenderFragment)

Renders the renderFragment and returns it as a IRenderedFragment.

public virtual IRenderedFragment Render(RenderFragment renderFragment)

Parameters

renderFragment RenderFragment

The render fragment to render.

Returns

IRenderedFragment

The IRenderedFragment.

RenderComponent<TComponent>(params ComponentParameter[])

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

public virtual IRenderedComponent<TComponent> RenderComponent<TComponent>(params ComponentParameter[] parameters) where TComponent : IComponent

Parameters

parameters ComponentParameter[]

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.

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

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

public virtual IRenderedComponent<TComponent> RenderComponent<TComponent>(Action<ComponentParameterCollectionBuilder<TComponent>> parameterBuilder) 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.

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 IRenderedComponent<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>().

SetRendererInfo(RendererInfo?)

Sets the RendererInfo for the renderer.

public virtual void SetRendererInfo(RendererInfo? rendererInfo)

Parameters

rendererInfo RendererInfo
.NET Foundation

Supported by the .NET Foundation.