BrowserFileExtensions.RequestImageFileAsync broken with bUnit 1.30.3
candritzky opened this issue · 4 comments
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.
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.
Hmmm - well that happened before, but didn't bubble up. We added the following line:
+ if (uploadTask.Exception is { InnerException: not null } e)
+ {
+ ExceptionDispatchInfo.Capture(e.InnerException).Throw();
+ }
@egil We might want to limit bubbling up only that one exception.
As you said @candritzky - we can't do much about that code the Blazor team is using.
@egil Opened a PR so we "just" bubble bUnit created Exceptions.
There is a preview version that will fix your issue, if you can get your hands on it and let us know, that would be amazing: https://www.nuget.org/packages/bunit/1.31.1-preview
@linkdotnet Thanks Steven, I tried with 1.31.1-preview and it solves my problem. Tests are green again. :-)