Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
  • May 17 23:12
    egil synchronize #397
  • May 17 22:32
    egil edited #396
  • May 17 21:36
    egil edited #396
  • May 17 17:42
    egil commented #396
  • May 17 17:42
    egil edited #397
  • May 17 17:42
    egil edited #397
  • May 17 17:41
    egil edited #397
  • May 17 17:41
    egil edited #397
  • May 17 17:41
    egil edited #397
  • May 17 17:41
    egil opened #397
  • May 17 16:17
    FlorianRappl commented #396
  • May 17 16:17
    FlorianRappl commented #396
  • May 17 15:46
    egil commented #396
  • May 17 15:41
    egil edited #396
  • May 17 15:41
    egil edited #396
  • May 17 15:34
    egil edited #396
  • May 17 14:45
    egil edited #396
  • May 17 14:16
    egil edited #396
  • May 17 13:42
    egil edited #396
  • May 17 13:37
    egil edited #396
Seppe_Geerinckx
@ClumsyPenguin
 var comp = ctx.RenderComponent<MudDialogProvider>();
            comp.Markup.Trim().Should().BeEmpty();
            DialogService service = ctx.Services.GetService<IDialogService>() as DialogService;
            service.Should().NotBe(null);
            IDialogReference dialogReference = null;
            await comp.InvokeAsync(() => dialogReference = service?.Show<CaseForm>());
`
I essentially render a MudDialogProvider to then draw my Caseform, but you can only set parameters when you render the component
How can i set these params?
[CascadingParameter] MudDialogInstance MudDialog { get; set; }
[Parameter] public bool isEdit { get; set; }
[Parameter] public SheCaseType caseType { get; set; }
Egil Hansen
@egil
comp.SetParametersAndRender has the same parameter passing options as RenderComponent. Lots of docs on this topic here: https://bunit.egilhansen.com/docs/providing-input/passing-parameters-to-components.html?tabs=csharp
also, consider using comp.MarkupMatches("") instead so you do not have to deal with insignificant whitespace. See https://bunit.egilhansen.com/docs/verification/verify-markup.html
Egil Hansen
@egil
ahh forgot to tag you @ClumsyPenguin, let me know if you have any more questions.
Kevin Burton
@KevinBurton
I have a test of which I have included a snippet
var cut = RenderComponent<ProjectPicker>(); cut.WaitForState(() => cut.Find("label").TextContent == "Project: ");
I get an exception that the 'label' could not be found. As I understand it this statement is supposed to wait for it to appear?
Egil Hansen
@egil
@KevinBurton no, WaitForState will wait until the predicate you pass to returns true. If it throws, it will stop waiting. I am planning to add a WaitForElement(cssSelector) that will do that for you. For now, you need to write cut.WaitForState(() => cut.FindAll("label").FirstOrDefault()?.TextContent == "Project: "); or use cut.WaitForAssertion(() => Assert.Equal("Project: ", cut.Find("label").TextContent);
Kevin Burton
@KevinBurton
So rather than FindAll, can I just do this cut.WaitForState(() => cut.Find("label")?.TextContent == "Project: ");?
Egil Hansen
@egil
no, because find will throw if it doesnt find the element, whereas findall simply returns an empty list.
waitforassertion will wait until the method
doesnt throw
Seppe_Geerinckx
@ClumsyPenguin
@egil thank you, i finished my first complete test suite yesterday. Bunit is an awesome tool for unit test blazor components. Cheers
Egil Hansen
@egil
awesome, happy to hear it!
Egil Hansen
@egil
Hey folks, I'm going live in 5 minutes with some bUnit testing of the Blazing Pizza Workshop. This time we will go over the authentication and authorization chapter from the workshop. Join the fun at twitch.tv/egilhansen
Egil Hansen
@egil
Hey @hopstarDev_twitter, with the new feature in the dev branch of bUnit, did you get a better integration with autofixture and bUnit working?
IIRC, you did mention that you had some issues, but I cannot remember if they have been solved.
Richard Sinden
@RichardSinden

Hi, I have some components that I've been trying to test with bUnit, but I've been having a problem with WaitForState timing out long after my async method had returned and caused the predicate it was testing to become true. This turned out to be because OnAfterRender was not being called, because the component I was testing also had a child component which was rendered only when the predicate became true too. The child component had a ViewModel injected into it and I had forgotten to add this to the Services in the test, so the failure was valid. The problem was that this took me ages to track down, because I couldn't see this exception anywhere.
If I comment out the ViewModel registration in Services and run the Blazor app then I get the following exception in the browser console:

blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
      Unhandled exception rendering component: Cannot provide a value for property 'ViewModel' on type 'MyThing.Client.Pages.ListItemView'. There is no registered service of type 'MyThing.Client.Pages.ListItemViewModel'.
System.InvalidOperationException: Cannot provide a value for property 'ViewModel' on type 'MyThing.Client.Pages.ListItemView'. There is no registered service of type 'MyThing.Client.Pages.ListItemViewModel'.
   at Microsoft.AspNetCore.Components.ComponentFactory.<>c__DisplayClass6_0.<CreateInitializer>g__Initialize|2(IServiceProvider serviceProvider, IComponent component)
   at Microsoft.AspNetCore.Components.ComponentFactory.PerformPropertyInjection(IServiceProvider serviceProvider, IComponent instance)
   at Microsoft.AspNetCore.Components.ComponentFactory.InstantiateComponent(IServiceProvider serviceProvider, Type componentType)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.InstantiateComponent(Type componentType)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.InstantiateChildComponentOnFrame(RenderTreeFrame& frame, Int32 parentComponentId)
   at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewComponentFrame(DiffContext& diffContext, Int32 frameIndex)
   at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewSubtree(DiffContext& diffContext, Int32 frameIndex)
   at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InsertNewFrame(DiffContext& diffContext, Int32 newFrameIndex)
   at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InsertNewFrame(DiffContext& diffContext, Int32 newFrameIndex)
   at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext& diffContext, Int32 oldStartIndex, Int32 oldEndIndexExcl, Int32 newStartIndex, Int32 newEndIndexExcl)
   at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.ComputeDiff(Renderer renderer, RenderBatchBuilder batchBuilder, Int32 componentId, ArrayRange`1 oldTree, ArrayRange`1 newTree)
   at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderInExistingBatch(RenderQueueEntry renderQueueEntry)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()
d.printErr @ blazor.webassembly.js:1
d.preRun.push.window.Blazor._internal.dotNetCriticalError @ blazor.webassembly.js:1
w @ blazor.webassembly.js:1
...

When I have the same issue with bUnit (with a the xUnit logger service added) I can't see this exception anywhere. I only figured it out because it stopped generating RenderingComponent messages at the component before the broken one. Is there any way to see this kind of exception in the logs, or is this out of bUnit's control?

Here is an example from the xUnit logs, where it just stops:
...
Debug | 6 - 8 | 2021-03-09T12:30:10.3396203Z | Microsoft.AspNetCore.Components.RenderTree.Renderer | 1:InitializingChildComponent | Initializing component 12 (MatBlazor.MatIcon) as child of 8 (MatBlazor.MatIconButton)
Debug | 6 - 8 | 2021-03-09T12:30:10.3399801Z | Microsoft.AspNetCore.Components.RenderTree.Renderer | 3:RenderingComponent | Rendering component 9 of type Telerik.Blazor.Components.Common.TrialMessage
Debug | 6 - 8 | 2021-03-09T12:30:10.3405994Z | Microsoft.AspNetCore.Components.RenderTree.Renderer | 3:RenderingComponent | Rendering component 10 of type Microsoft.AspNetCore.Components.CascadingValue`1[MatBlazor.MatAccordion]
Debug | 6 - 8 | 2021-03-09T12:30:40.3128536Z | Bunit.Extensions.WaitForHelpers.WaitForHelper | 5:OnTimeout | The wait for helper for component 1 timed out
Debug | 6 - 8 | 2021-03-09T12:30:40.3138269Z | Bunit.Extensions.WaitForHelpers.WaitForHelper | 6:Dispose | The state wait helper for component 1 disposed
...
Egil Hansen
@egil
Hi @RichardSinden, sorry for the troubles. It could be related to this issue, which I have on my todo list to fix before the next release: egil/bUnit#319
Richard Sinden
@RichardSinden
Oh yeah, sounds like it might well be. I did have a look through the issues but somehow completely missed that one. Thanks, @egil. I've only just started playing with bUnit, but I'm really liking it so far.
Egil Hansen
@egil
Thats great to hear. Let me know if you run into other issues.
Egil Hansen
@egil
Hey folks, want to help me finish bUnit, or just AMA, then now is your chance. Join me at https://www.twitch.tv/egilhansen
Jakob Reesalu
@jensengbg-jakob-reesalu
Hey! I'm a newbie. I was planning to test that a click on an a-tag navigates to another page. I thought it'd just be to verify there's a change in the CUT's markup, but I realized the navigation (of course) redirects to a new page and "leaves" the CUT. Is this sort of test just outside the scope of bUnit?
Egil Hansen
@egil
Hi @jensengbg-jakob-reesalu, you can verify test that. You need a fake navigation manager that the component can invoke the NavigateTo method on, and then you can use your fake navigation manager to verify that the navigation request happened.
hang on, let me dig up an example
Then you just need to register it with the TestSontext's services collection, e.g.: ctx.Services.AddSingleton<NavigationManager, FakeNavigationManager>()
Jakob Reesalu
@jensengbg-jakob-reesalu
@egil Thanks for your reply! Ya, I already implemented something like this, I found an old thread where you discussed this with someone and they provided their fake navigator there. Sorry, I should've said this so you wouldn't spend time on that! But ok, then I know the scope of this kind of test :)
Still nice to see that page with all those tests, I might use it to find other answers later on ^^
Jakob Reesalu
@jensengbg-jakob-reesalu
Actually, your fakemanager was a little cleaner so I'll shamelessly steal it xP
Egil Hansen
@egil
haha there is no shame in that, its there for the taking.
there is a whole series of videos of me building all the tests you see in that blazor-workshop repo: https://www.youtube.com/channel/UCcHRuajIkdHagmpdkE01YPg
if you want to learn more.
Felipe Costa Gualberto
@FelipeCostaGualberto
Hello! How can I test a component in a specific route?
Felipe Costa Gualberto
@FelipeCostaGualberto
I figured out. Just pass the route's arguments as parameters.
Egil Hansen
@egil
great, glad you sorted it out.
Egil Hansen
@egilhansen:matrix.org
[m]
Test from Elements client
Thomas Castiglione
@gulbanana
just dropping in to say that bUnit continues to work very well. thanks egil and everyone who's contributed - this tool really helps my projects out
Egil Hansen
@egilhansen:matrix.org
[m]
Awesome @gulbanana
George Fabish
@thepigeonfighter
I've heard a lot about this project and am excited to get to use it today. So far its working fantastic. Great job! I see in the docs how to fake authorization in unit tests in C# but is there a way to do that as simply in Razor?
George Fabish
@thepigeonfighter
I think i may have got it to work by just copying this code and reapplying it for the Fixture type. Not sure if that is the cleanest solution though.
https://github.com/egil/bUnit/blob/74fd44bf548417bfb75971d498a996816387904f/src/bunit.web/TestDoubles/Authorization/FakeAuthorizationExtensions.cs#L16
Egil Hansen
@egil
@thepigeonfighter you should be able to do it with <Fixture> style tests as well. Just call fixture.AddTestAuthorization()before calling fixture.GetComponentUnderTest()
A Fixture is a TestContextBase.
George Fabish
@thepigeonfighter
If it was a snake it would have bit me, can't believe I missed that. Thanks.
Egil Hansen
@egil
@thepigeonfighter yeah, its not super obvious nor well documented, so I think you are excused. Another feature I need to document is an new approach to writing tests in razor components. You can see examples of this in this sample's tests project: https://github.com/egil/blazor-workshop.
George Fabish
@thepigeonfighter
Great! I'll check that out thanks.
George Fabish
@thepigeonfighter
One more issue I just ran into is when using an EditForm component with a button as a child element the EditForm get triggered by clicking on that button. But if I try to do a "Click" via bUnit it says there is no click handler registered, which makes sense. But not sure how to submit the form via bUnit's api?
George Fabish
@thepigeonfighter
Sorry my fault I just saw an example of this is the workshop link you shared. Thanks.