A testing library for Blazor components. Get started testing Blazor components at bunit.egilhansen.com/docs/getting-started/
cut.WaitForAssertion(() => /* assertion goes here */)
since the test code runs in one thread and the component and renderer in another, to avoid deadlocks.
[Inject]
private IState<CounterState> CounterState { get; set; } https://github.com/mrpmorris/Fluxor/blob/master/Tutorials/02-Blazor/02A-StateActionsReducersTutorial/README.md
IState
, and then in your test, write Services.AddSingleton<IState<CounterState>>(new FakeState(....))
cut.FindComponent<TypeOfComponent>()
var menu = cut.FindComponent<ContextMenu>()
and then inspect menu.Instance
to see what options you have on the component.
So some times I really want to kick myself for not thinking of something earlier. This is one of those times. This morning I realized that the inline render fragment syntax in razor has the potential for much better razor based tests.
Have small spike, where this is possible:
@inherits TestComponentBase2
@code
{
[Fact]
public void Test1()
{
var cut = RenderFragment(@<div>hello world</div>);
cut.MarkupMatches(@<div>hello world</div>);
}
[Fact]
public void Test2()
{
var cut = RenderComponent<Counter>(@<Counter DarkMode="true">
</Counter>);
Assert.True(cut.Instance.DarkMode);
}
[Fact]
public void Counter_Increments_When_Button_Is_Clicked()
{
var cut = RenderFragment(@<Counter DarkMode="true" />);
cut.Find("button").Click();
cut.Find("p").MarkupMatches(@<p>Current count: 1</p>);
}
}
What do you guys think?
cut.Render()
or cut.SetParametersAndRender(...)
if you need to pass new parameters.