How can I set a CascadingParameter to be the class for a subcomponent? #934
Replies: 1 comment 1 reply
-
Hey @RonnieGarduno, I hope you are doing fine. I just have a rough plan in my head of what your code looks like, so correct me if I am wrong here. The Now I am a bit confused by your edit, where you stated you are using a stub of I saw that you initialize your component like this: parameters.Add(p => p.Layout, new MainLayout()) Where this works without question, as a Blazor component is just a regular C# class, it will bypass all stages of initialization of the Blazor-Renderpipeline. So basically you have an empty container. That would explain the exception you get. Now there are multiple things you can do. The easiest, depending on your circumstances, would be the render using var ctx = new TestContext();
var mainLayout = ctx.RenderComponent<MainLayout>(p => p.AddChildContent<Bullseye>()));
var cut = mainLayout.FindComponent<Bullseye>(); More information you can find here. Regarding cascading parameters, I would advice checking the linked page above as well. There are lots of samples, which can push you in the right direction. Let me know if that helps you in any way. |
Beta Was this translation helpful? Give feedback.
-
EDIT: Got it working on my own. All I needed was
ctx.RenderTree.TryAdd<MainLayout>();
in the end. Hooray for working tests!Hi all. I'm still learning bUnit, so I hope this is an easy question to answer.
I have a
MainLayout.razor
file with the following property & field defined in it:private bool sidebar = false; public bool Sidebar { get { return sidebar; } set { sidebar = value; InvokeAsync(StateHasChanged); } }
The problem I'm facing is that I am trying to test a component called
BullseyePage.razor
which has aMainLayout
as aCascadingParameter
.[CascadingParameter] public MainLayout Layout { get; set; }
When I try to render a
BullseyePage
to test it:cut = ctx.RenderComponent<WebApplication1.Pages.BullseyePage>(parameters => parameters.Add(p => p.Layout, new MainLayout()));
I get an error during runtime stating:
System.InvalidOperationException: 'The render handle is not yet assigned.'
.The line of code referenced is the part of
MainLayout.razor
above inside theset
forSidebar
where weInvokeAsync
theStateHasChanged
method. That part is there to ensure that the component re-renders when it sets the Sidebar property.I've tried all manner of things. My main thought is that I should be able to call
RenderComponent
for a newMainLayout
object, then get theMainLayout
itself out of the rendered fragment, but I haven't been able to get that to work.I can add more code here if it will help, but please let me know if you understand what's going on here. Thanks for your consideration.
EDIT: I forgot to mention that I am trying to stub this object out. I will use a real Mock if I have to, but I am using this, but not getting what I want:
ctx.ComponentFactories.AddStub<MainLayout>();
It is also probably worth noting that the MainLayout has itself as a CascadingParameter, so it can be used by child components. This part has me going in circles.
Beta Was this translation helpful? Give feedback.
All reactions