Skip to content

BrowserFileExtensions.RequestImageFileAsync broken with bUnit 1.30.3 #1518

@candritzky

Description

@candritzky

Describe the bug

We are testing Blazor code that calls the BrowserFileExtensions.RequestImageFileAsync API.
After upgrading to bUnit 1.30.3, the call fails because the AspNetCore.Components code checks if the IBrowserFile reference is of (implementation) type BrowserFile.

image

Example:
Testing this component:

            <InputFile data-testid="file-input" class="file-upload" OnChange="this.OnFileChangedAsync" />
    private async Task OnFileChangedAsync(InputFileChangeEventArgs e)
    {
        IBrowserFile file = e.File;
        IBrowserFile previewFile = await file.RequestImageFileAsync(file.ContentType, PreviewSize, PreviewSize);
       ...

With this test:

    [Fact]
    public async Task Test()
    {
        IRenderedComponent<MyFileUpload> cut = this.RenderComponent<MyFileUpload>();

        IRenderedComponent<InputFile> fileComponent = cut.FindComponent<InputFile>();

        await fileComponent.InvokeAsync(() =>
            fileComponent.UploadFiles(InputFileContent.CreateFromBinary([1, 2, 3, 4], "image.png")));
    }

Results in this output:

System.InvalidOperationException
Cannot perform this operation on custom Microsoft.AspNetCore.Components.Forms.IBrowserFile implementations.
   at Microsoft.AspNetCore.Components.Forms.BrowserFileExtensions.RequestImageFileAsync(IBrowserFile browserFile, String format, Int32 maxWidth, Int32 maxHeight)
   at MyFileUpload.OnFileChangedAsync(InputFileChangeEventArgs e) in MyFileUpload.razor.cs:line 98

Expected behavior:

Test should succeed.

Version info:

  • bUnit version: 1.30.3 (worked with 1.29.5)
  • .NET Runtime and Blazor version: 8.0.8
  • OS type and version: Windows 11, Edge
  • MudBlazor 6.21.0

Additional information:

No idea how this could ever have worked as BUnitBrowserFile always just implemented IBrowserFile, but did not derive from BrowserFile.
This is certainly a violation of LSP (Liskov Substitution Principle) on Microsoft/Blazor side.
But I can't understand why the test works with bUnit up to version 1.29.5.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions