A testing library for Blazor components. Get started testing Blazor components at bunit.egilhansen.com/docs/getting-started/
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.
IRenderedComponent<Page> _RenderedPage = context.RenderComponent<Page>(); // initialised early in test
//in assertion phase
var markup = _RenderedPage.Markup; //<-------- This markup has exactly what I expect for the Form component.
var form = _RenderedPage.FindComponent<Form>();
var formMarkup = form.Markup; // <---------- This markup is different. It has outdated values
It's as though something is internally caching that form and giving me an older state
There is a single RenderTree that is updated when a component renders. But what can happen is that your changes are happening asynchronously... put differently, the blazor renderer that is updating the render tree is updating it in a different thread than the one the test is running in, so the changes might be coming, but might not be there yet. The problem you are seeing might be due to beta-10 though. There was some changes to have the render tree updates would update the markup in the components, and you might be seeing a corner case I believe is fixed now.