Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SetParametersAndRender will breaks with .NET 8 #1165

Closed
a2er opened this issue Jul 30, 2023 · 7 comments · Fixed by #1166
Closed

SetParametersAndRender will breaks with .NET 8 #1165

a2er opened this issue Jul 30, 2023 · 7 comments · Fixed by #1166
Assignees
Labels
bug Something isn't working

Comments

@a2er
Copy link

a2er commented Jul 30, 2023

Describe the bug
The extension function SetParametersAndRender throws a NullPointerException since the ComponentState.SetDirectParameters methods vilisbility is changed to internal in .NET 8.0 Preview 4.

The exception is raised here:

var componentState = GetRequiredComponentStateMethod.Invoke(this, new object[] { renderedComponent.ComponentId })!;
var setDirectParametersMethod = componentState.GetType().GetMethod("SetDirectParameters", BindingFlags.Public | BindingFlags.Instance)!;
setDirectParametersMethod.Invoke(componentState, new object[] { parameters });

Example:
Testing this component:

<div>
    @Test
</div>

@code {
    [Parameter]
    public string? Test { get; set; }
}

With this test:

        // Arrange
        var test = "Hello";
        var nextTest = "World";

        // Act
        var cut = RenderComponent<ExampleComponent>(p => p.Add(x => x.Test, test));
        cut.SetParametersAndRender(p => p.Add(x => x.Test, nextTest));

        // Assert
        cut.MarkupMatches($"<div>{nextTest}</div>");

Results in this output:

Message: 
System.NullReferenceException : Object reference not set to an instance of an object.

  Stack Trace: 
<>c__DisplayClass32_0.<SetDirectParameters>b__0() line 213
<>c.<InvokeAsync>b__8_0(Object state)
--- End of stack trace from previous location ---
TestRenderer.AssertNoUnhandledExceptions() line 582
TestRenderer.SetDirectParameters(IRenderedFragmentBase renderedComponent, ParameterView parameters) line 232
RenderedComponentRenderExtensions.SetParametersAndRender[TComponent](IRenderedComponentBase`1 renderedComponent, ParameterView parameters) line 33
RenderedComponentRenderExtensions.SetParametersAndRender[TComponent](IRenderedComponentBase`1 renderedComponent, Action`1 parameterBuilder) line 68
Tests.UpdatesCorrectly() line 39
RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
MethodInvoker.Invoke(Object obj, IntPtr* args, BindingFlags invokeAttr)

Version info:

  • bUnit version: 1.22.19
  • .NET Runtime and Blazor version: 8.0.0-preview.6.23329.11

Additional context:

Related changes in dotnet/aspnetcore:

@linkdotnet
Copy link
Collaborator

Hey @a2er,

Yes that is definitely a regression we did not catch as we only run the test up til net7.0.
@egil as v2 is not ready, we should enable v1 for net8.0. At least to a degree where users can still use v1 without any issue on the newest .net version.

@linkdotnet
Copy link
Collaborator

@egil: I have a PR ready when I get back from the holidays (tomorrow evening).
I have a new laptop and my ssh settings and stuff are not ported - therefore I want to set this up tomorrow for signed commits and stuff

@egil
Copy link
Member

egil commented Jul 30, 2023

FYI: In net 8 some of the things we need reflection for are now available without.

@linkdotnet linkdotnet self-assigned this Jul 30, 2023
@linkdotnet linkdotnet added the bug Something isn't working label Jul 30, 2023
@a2er
Copy link
Author

a2er commented Jul 30, 2023

Thanks for the quick respone!

FYI: I was running the unit tests with .net 8. There are other issues:
grafik
(Fails related to this issue are not shown.)

Let me know if you want me to do further investigations on those.

@linkdotnet
Copy link
Collaborator

Thanks for the heads up. Yes there are other things for net8.0. Mainly the renderer has a new method that the bUnit one has to implement. And there is a new test double we have to provide.

@linkdotnet
Copy link
Collaborator

linkdotnet commented Jul 31, 2023

There should be a prerelease on NuGet soon.
Would be nice to get some feedback on this one

https://www.nuget.org/packages/bunit/1.23.1-preview

@a2er
Copy link
Author

a2er commented Aug 1, 2023

SetParametersAndRender is working as expected.
I will continue playing around with .NET 8 Blazor (but WASM only) you may hear from me.

Thanks for the quick fix guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants