Upgrading bunit to 1.2.49 causes tests to fail #474
-
I upgraded bunit today but now some tests using click events fail. Here is an example of a test: public void DocumentsGrid_DeleteButton()
{
InsertTestData(Services.GetService<IDbContextFactory<CAIRSContext>>());
//Act
IRenderedComponent<DocumentsGrid> cut = RenderComponent<DocumentsGrid>();
Renderer.Dispatcher.InvokeAsync(() => cut.Instance.SelectedAnimalChanged(1));
var deleteButton = cut.FindAll(".btn").Single(x => x.InnerHtml.Contains("Delete"));
deleteButton.Click();
TestSetupCheck(cut, MethodBase.GetCurrentMethod().Name + ".html");
//Assert
string expectedHtml = ExpectedResultsHelper.GetExpectedHtml(Path.Combine(testProjectRoot, "Components", "Grids", MethodBase.GetCurrentMethod().Name + ".html"));
cut.WaitForAssertion(() => cut.MarkupMatches(expectedHtml));
} The error is: However I am doing the FindAll and then Click together, so it should be working on the latest render (?). |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
The previous version of bUnit had a bug where it swallowed an error from the Blazor renderer, when an event handler that was previously attached to an element had been disposed. I am guessing you have something in your component under test that does some work asynchronously, perhaps Try this: public async Task DocumentsGrid_DeleteButton()
{
InsertTestData(Services.GetService<IDbContextFactory<CAIRSContext>>());
//Act
var cut = RenderComponent<DocumentsGrid>();
// Either do `await cut.InvokeAsync(...)` (btw. cut.InvokeAsync is a shorthand for Renderer.Dispatcher.InvokeAsync)
await cut.InvokeAsync(() => cut.Instance.SelectedAnimalChanged(1));
// Or do something like this, where you wait for some state that indicate that all async operations are done
cut.WaitForState(() => /* some check that verifies that has been done */);
var deleteButton = cut.FindAll(".btn").Single(x => x.InnerHtml.Contains("Delete"));
deleteButton.Click();
TestSetupCheck(cut, MethodBase.GetCurrentMethod().Name + ".html");
//Assert
var expectedHtml = ExpectedResultsHelper.GetExpectedHtml(Path.Combine(testProjectRoot, "Components", "Grids", MethodBase.GetCurrentMethod().Name + ".html"));
cut.WaitForAssertion(() => cut.MarkupMatches(expectedHtml));
} Let me know if this helps! |
Beta Was this translation helpful? Give feedback.
hi @robalexclark
The previous version of bUnit had a bug where it swallowed an error from the Blazor renderer, when an event handler that was previously attached to an element had been disposed.
I am guessing you have something in your component under test that does some work asynchronously, perhaps
SelectedAnimalChanged
, that causes a render after the work is done, e.g. by callingStateHasChanged
. Since you are not awaiting theInvokeAsync
operation, you effectively have a race condition between the code running in the test thread and the re-rendering of your CUT in the renderer.Try this: