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.12k stars 104 forks source link

upgrading bUnit to 1.27.17 results in bunit.web.testcomponents load errors #1441

Closed shalinparmar closed 5 months ago

shalinparmar commented 5 months ago

After upgrading to bUnit 1.27.17, existing test written using bunit.web.testcomponents is failing with the following error:

Method 'CreateTestRenderer' in type 'Bunit.SnapshotTest' from assembly 'Bunit.Web.TestComponents, Version=1.23.9.12610, Culture=neutral, PublicKeyToken=fe9c6306bcc1ce6f' does not have an implementation. Exception doesn't have a stacktrace

Upgrade to bunit latest package and have tests using bunit.web.testcomponents

<td>
    @if (IsSortedByCurrentColumn)
    {
        @if (OrderBy == OrderBy.Ascending)
        {
            <a href="" role="button" aria-label="@Label" aria-hidden="false"
               @onclick:preventDefault
               @onclick="@CustomEventCallback">

                @Label
                <i class="fas fa-sort-up"></i>
            </a>
        }
        else
        {
            <a href="" role="button" aria-label="@Label" aria-hidden="false"
                @onclick:preventDefault
                @onclick="@CustomEventCallback">
                @Label
                <i class="fas fa-sort-down"></i>
            </a>
        }
    }
    else
    {
        <a href="" role="button" aria-label="@Label" aria-hidden="false"
            @onclick:preventDefault
            @onclick="@CustomEventCallback">
            @Label
            <i class="fas fa-sort"></i>
        </a>
    }

</td>

With this test:

@inherits TestComponentBase

<SnapshotTest Description="Scenario 1: Custom sortable component renders correctly">
    <TestInput>
        <CustomSortableTD Label="Test Label" CustomEventCallback="CustomEventCallbackFunction" IsSortedByCurrentColumn='@false'/>
    </TestInput>
    <ExpectedOutput>
        <td>
            <a href="" role="button" aria-label="Test Label" aria-hidden="false"  >Test Label
                <i class="fas fa-sort"></i>
            </a>
        </td>
    </ExpectedOutput>
</SnapshotTest>

Results in this output:

System.InvalidOperationException
Exception thrown during razor test discovery on UnitTests.Shared.Components.CustomSortableTDUnitTests'.
Method 'CreateTestRenderer' in type 'Bunit.SnapshotTest' from assembly 'Bunit.Web.TestComponents, Version=1.23.9.12610, Culture=neutral, PublicKeyToken=fe9c6306bcc1ce6f' does not have an implementation.
  Exception doesn't have a stacktrace

Should work fine.

Version info:

linkdotnet commented 5 months ago

We changed some stuff in 1.27 around the renderer and how it is created. Also we dropped support for bunit.web.testcomponents completely in >1.23 (this was planned since release 1.0.16. That is why you see that version "mismatch".

So moving forward you would have to migrate those tests. A bit more information can be found in the CHANGELOG.md.

linkdotnet commented 5 months ago

Your given test could be easily rewritten as such:

public void MyFancyTestName()
{
  var cut = Render(@<CustomSortableTD Label="Test Label" CustomEventCallback="CustomEventCallbackFunction" IsSortedByCurrentColumn='@false'/>);

  cut.MarkupMatches(@<td>
            <a href="" role="button" aria-label="Test Label" aria-hidden="false"  >Test Label
                <i class="fas fa-sort"></i>
            </a>
        </td>);
}