If I have a Blazor component:
@code {
[Parameter] public EventCallback<string> OnEvent { get; set; }
}
And a bUnit test:
@code {
var cut = RenderComponent<MyComp>(builder =>
{
builder.Add(mc => mc.OnEvent, Handler("Fired"));
}
await cut.Instance.OnEvent.InvokeAsync();
private void Handler(string val) {}
}
Two questions:
- Can I convert this to use an arbitrary method defined and setup using Moq?
I have tried:
Mock<EventHandler<string>> mockHandler = new Mock<EventHandler<string>>();
mockHandler.Setup(m => m.X(It.IsAny<object>(), "Fired"));
But I can't figure out how to pass the mockHandler method to the component.
- How can I verify that OnEvent is fired?
Dont call EventCallbacks directly in your tests, trigger them as you would through a user interaction.
E.g. to test the EventCallback in the following component:
Do this:
Learn more over on https://bunit.dev/docs/getting-started