IvanJosipovic / BlazorTable

Blazor Table Component with Sorting, Paging and Filtering
https://BlazorTable.netlify.app
MIT License
534 stars 109 forks source link

Unit Testing Blazor WASM with BlazorTable component #299

Closed dondeetan closed 3 years ago

dondeetan commented 3 years ago

Hi Ivan,

Question: We are working on unit testing our blazor wasm app and first of all, your component is very nice!!! Kudos to you. Made our lives easier.

Anyway, we have a problem when we unit test (using BUnit for Blazor), we get the following exception (See exception below)

At first, we had the same problem with ILogger but we fixed this by adding Inject on the razor page but for localization, we can't seem to provide the correct syntax, injection, etc...

Need your guidance on the below exception.

Note: We are using the latest blazortable component.

Thank you!

Error Message: System.InvalidOperationException : Cannot provide a value for property 'Localization' on type 'BlazorTable.Table1[[SecOpsIntakePortal.Common.Entities.AzureResourceGroup, SecOpsIntakePortal.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]'. There is no registered service of type 'Microsoft.Extensions.Localization.IStringLocalizer1[BlazorTable.Components.Localization]'. Stack Trace: at Microsoft.AspNetCore.Components.ComponentFactory.<>c__DisplayClass6_0.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.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, ArrayRange1 oldTree, ArrayRange1 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() --- End of stack trace from previous location --- at Bunit.Rendering.TestRenderer.AssertNoUnhandledExceptions() in /
/src/bunit.core/Rendering/TestRenderer.cs:line 315 at Bunit.Rendering.TestRenderer.Render[TResult](RenderFragment renderFragment, Func`2 activator) in //src/bunit.core/Rendering/TestRenderer.cs:line 197 at Bunit.Rendering.TestRenderer.RenderFragment(RenderFragment renderFragment) in //src/bunit.core/Rendering/TestRenderer.cs:line 43 at Bunit.Extensions.TestContextExtensions.RenderInsideRenderTree[TComponent](TestContextBase testContext, RenderFragment renderFragment) in //src/bunit.web/Extensions/TestContextExtensions.cs:line 21 at Bunit.TestContext.Render[TComponent](RenderFragment renderFragment) in //src/bunit.web/TestContext.cs:line 64 at Bunit.TestContext.RenderComponent[TComponent](ComponentParameter[] parameters) in /_/src/bunit.web/TestContext.cs:line 37 at SecOpsIntakePortal.UI.Tests.AzurePageTests.AzureComponent_AuthenticatedAccess_RendersAuthContent() in C:\SourceControl\VWAC\SecOps\SecOpsIntakePortal.UI.Tests\AzurePageTests.cs:line 109 --- End of stack trace from previous location ---

IvanJosipovic commented 3 years ago

Hey,

You will need to run .AddBlazorTable() on the Bunit TestContext. See here, https://bunit.dev/docs/providing-input/inject-services-into-components.html#injecting-services-in-tests

If it still doesn't work, do you mind posting a simplified example of one Test?

anitagov commented 3 years ago

Thanks a bunch @IvanJosipovic . Yes, this worked. @dondeetan and I posted this a short while back. Thanks for the prompt response.

dondeetan commented 3 years ago

Thanks all. This makes total sense. How could we have missed this :). Forgot to declare that. Thanks a bunch and thanks @anitagov for confirming.