bUnit-dev / bUnit

bUnit is a testing library for Blazor components that make tests look, feel, and runs like regular unit tests. bUnit makes it easy to render and control a component under test’s life-cycle, pass parameter and inject services into it, trigger event handlers, and verify the rendered markup from the component using a built-in semantic HTML comparer.
https://bunit.dev
MIT License
1.14k stars 105 forks source link

BUnit does not work with Blazorise Components #1023

Closed johniwasz closed 1 year ago

johniwasz commented 1 year ago

Testing any component that that includes a component from Blazorise fails with:

System.InvalidOperationException : Cannot consume scoped service 'Microsoft.AspNetCore.Components.IComponentActivator' from singleton 'Bunit.Rendering.WebTestRenderer'.

OR:

System.InvalidOperationException : Cannot provide a value for property 'JSModule' on type 'Blazorise.Button'. There is no registered service of type 'Blazorise.Modules.IJSButtonModule'.

Example: Testing this component:

@using Blazorise

<h1>Counter</h1>

<p role="status">Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

<Button Clicked="IncrementCount">Click Me (Blazorise)</Button>

With this test:

        [Fact]
        public void CounterStartsAtZero()
        {            
            // Arrange
            var cut = RenderComponent<Counter>();

            // Assert that content of the paragraph shows counter at zero
           cut.Find("p").MarkupMatches("<p role=\"status\">Current count: 0</p>");
        }

Results in this output:

 System.InvalidOperationException : Cannot provide a value for property 'JSModule' on type 'Blazorise.Button'. There is no registered service of type 'Blazorise.Modules.IJSButtonModule'.

Or this test:

        [Fact]
        public void CounterStartsAtZero()
        {
            Services
                .AddBlazorise(options =>
                {
                    options.Immediate = true;
                })
                .AddBootstrapProviders()
                .AddFontAwesomeIcons();

            // Arrange
            var cut = RenderComponent<Counter>();

            // Assert that content of the paragraph shows counter at zero
           cut.Find("p").MarkupMatches("<p role=\"status\">Current count: 0</p>");
        }

Results in this output:

 System.InvalidOperationException : Cannot consume scoped service 'Microsoft.AspNetCore.Components.IComponentActivator' from singleton 'Bunit.Rendering.WebTestRenderer'.

Expected behavior:

This line results in successful instantiation of the Counter componet:

        var cut = RenderComponent<Counter>();

Version info:

Additional context:

A repo with a reproduction of this error can be found here:

https://github.com/johniwasz/blazorise-bunit-repro

egil commented 1 year ago

That's odd, since I know the folks from Blazorise (@Megabit) use bUnit to test Blazorise with themselves 🙂

Perhaps @stsrki has a tutorial on how to get started testing components that use Blazorise components.

egil commented 1 year ago

Ill convert this to a QA discussion.