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

The render handle is not yet assigned. #45

Closed jpiell closed 4 years ago

jpiell commented 4 years ago

I am getting an exception when trying to render my component.

Any directional help would be appreciated.

Here is my test:


[Fact(DisplayName = "Test to see the Lookup Renders Properly - Select Grid Item")]
        public void LookupRendersCorrectlySelectGridItem()
        {
            // Arrange
            var active = true;
            var id = 1;
            var description = "Item 1";
            var name = "Item1";
            var lookupEntities = new List<LookupViewModel>() { new LookupViewModel() { Active = active, Id = id, Description = description, Name = name } };
            var tipClient = new Mock<ITipClient>();
            tipClient.Setup(x => x.GetJsonAsync<List<LookupViewModel>>(It.IsAny<string>())).Returns(Task.FromResult(lookupEntities));

            Services.AddSingleton<ITipClient>(tipClient.Object);
            Services.AddMockJsRuntime();
            Services.AddTelerikBlazor();

            var root = new TelerikRootComponent();

            // Act
            var cut = RenderComponent<Lookup>(
                CascadingValue(root),
                CascadingValue("GridData", lookupEntities)
                );

            // Assert
            Assert.NotNull(cut);
        }
egil commented 4 years ago

What version are you on? Can you include the stack trace? What is the lookup component? Please share it if possible.

jpiell commented 4 years ago

5.1

System.InvalidOperationException HResult=0x80131509 Message=The render handle is not yet assigned. Source=Microsoft.AspNetCore.Components StackTrace: at Microsoft.AspNetCore.Components.RenderHandle.ThrowNotInitialized() at Microsoft.AspNetCore.Components.RenderHandle.Render(RenderFragment renderFragment) at Microsoft.AspNetCore.Components.ComponentBase.StateHasChanged() at Telerik.Blazor.Components.TelerikRootComponentBase.AddFragment(RenderFragment renderFunction) at Telerik.Blazor.Components.RootComponent.TelerikRootComponentFragmentBase.OnInitializedAsync() at Microsoft.AspNetCore.Components.ComponentBase.d__20.MoveNext() at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at Egil.RazorComponents.Testing.TestRenderer.AssertNoSynchronousErrors() at Egil.RazorComponents.Testing.TestRenderer.DispatchAndAssertNoSynchronousErrors(Action callback) at Egil.RazorComponents.Testing.ContainerComponent.Render(RenderFragment renderFragment) at Egil.RazorComponents.Testing.RenderedFragmentBase..ctor(ITestContext testContext, RenderFragment renderFragment) at Egil.RazorComponents.Testing.RenderedComponent1..ctor(ITestContext testContext, RenderFragment renderFragment) at Egil.RazorComponents.Testing.RenderedComponent1..ctor(ITestContext testContext, IReadOnlyList`1 parameters) at Egil.RazorComponents.Testing.TestContext.RenderComponentTComponent at TIPS.ComponentTests.CommonComponentTests.LookupRendersCorrectlySelectGridItem() in C:\Projects\TIPS\ComponentTests\CommonComponentTests\ComponentTests.cs:line 257

This exception was originally thrown at this call stack: [External Code]

TIPS.ComponentTests.CommonComponentTests.LookupRendersCorrectlySelectGridItem() in ComponentTests.cs

On Fri, Jan 24, 2020 at 12:11 PM Egil Hansen notifications@github.com wrote:

What version are you on, and can you include the stack trace?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/egil/razor-components-testing-library/issues/45?email_source=notifications&email_token=AHE7FB6OEV2VMEFZVH7M363Q7MOKPA5CNFSM4KLJALP2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEJ3OTBY#issuecomment-578218375, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHE7FBY7JPITHNHAOGG3DMTQ7MOKPANCNFSM4KLJALPQ .

egil commented 4 years ago

Hmm, sorry, can't see what's going on. Can you create a minimal example that shows the problem?

egil commented 4 years ago

I am closing this for now. If this is still an issue, just get back to me with a minimal example.

jpiell commented 4 years ago

Hi Egil,

Sorry I got tied up in other things.

Attached is an example.

Thanks,

Jeff TelerikBlazorApp1.zip

jpiell commented 4 years ago

Keep it closed. It was my error.

jpiell commented 4 years ago

Nope = I still need help. The problem occurs if the RenderComponent passes the component and not the grid. See the sample I sent.

jpiell commented 4 years ago

Ok now I did figure it out.

The render should look like this:

        var cut = RenderComponent<TelerikRootComponent>(
            ChildContent<FetchData>(
            ));
egil commented 4 years ago

Hi @jpiell

Good. Great it worked out. If you want to make it a bit easier to test your components the rely on Teleriks components, you can add a small helper method like this in your test class:

private IRenderedComponent<TComponent> RenderComponentInTelerikRoot<TComponent>(params ComponentParameter[] parameters) where TComponent : class, IComponent
{
    return RenderComponent<TelerikRootComponent>(ChildContent<TComponent>(parameters));
}

Then you can do simply do a:

var cut = RenderComponentInTelerikRoot<FetchData>();
jpiell commented 4 years ago

Nice!

From: Egil Hansen notifications@github.com Sent: Thursday, January 30, 2020 4:43 PM To: egil/razor-components-testing-library razor-components-testing-library@noreply.github.com Cc: jpiell jpiell@gmail.com; Mention mention@noreply.github.com Subject: Re: [egil/razor-components-testing-library] The render handle is not yet assigned. (#45)

Hi @jpiell https://github.com/jpiell

Good. Great it worked out. If you want to make it a bit easier to test your components the rely on Teleriks components, you can add a small helper method like this in your test class:

private IRenderedComponent RenderComponentInTelerikRoot(params ComponentParameter[] parameters) where TComponent : class, IComponent { return RenderComponent(ChildContent(parameters)); }

Then you can do simply do a:

var cut = RenderComponentInTelerikRoot();

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/egil/razor-components-testing-library/issues/45?email_source=notifications&email_token=AHE7FB4JFNPO4IAKMKCPFD3RANCXTA5CNFSM4KLJALP2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEKMVKBI#issuecomment-580474117 , or unsubscribe https://github.com/notifications/unsubscribe-auth/AHE7FB32RY2NI74O7KWFKKDRANCXTANCNFSM4KLJALPQ . https://github.com/notifications/beacon/AHE7FB2U5FSMDJTDSEJ2EYTRANCXTA5CNFSM4KLJALP2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEKMVKBI.gif

RafaelMeloTrakx commented 2 years ago

The same error happens to me when I try to add a simple menu item dynamically. I am using Radzen.Blazor v3.9.6.

image

image

I hope we can get a more stable version of Radzen components for Blazor soon in https://github.com/radzenhq/radzen-blazor.

egil commented 2 years ago

@RafaelMeloTrakx you really need to provide a lot more details for me to help. Ideally a minimal project that has just enough code to reproduce your problem.

Modricagon commented 2 years ago

I am having this issue it has been very challenging.

I have included a minimal example to show what has been happening.

Modricagon commented 2 years ago

ProblemExample.zip

egil commented 2 years ago

@Modricagon you are new'ing up the Modal component directly. That is not supported by Blazor and will result in the exception you are seeing. This is not really a bUnit thing, this is fundamental to how Blazor works.

Modricagon commented 2 years ago

@Modricagon you are new'ing up the Modal component directly. That is not supported by Blazor and will result in the exception you are seeing. This is not really a bUnit thing, this is fundamental to how Blazor works.

Thanks for this! This really helped me understand what I was doing wrong. Instead I rendered the Modal component first with the "var cut = ctx.RenderComponent();" and then passed the "cut.Instance".

Thanks so much issue resolved.

johndhunter commented 1 year ago

@Modricagon - I have this exact same issue - any chance you could expand on what you did to resolve it please? i.e. How did you change:

[Test]
public void Test1()
{  
    ctx.RenderComponent<Wizard>(parameters =>
    {
        parameters.Add(p => p.ModalInstance, new Modal());
    });
}