Skip to content

Unable to resolve service for type ILogger #405

@InzeNL

Description

@InzeNL

Describe the bug
I am running into a problem trying to test my Blazor app which uses Blazorise. When I try to render my component Index (initially occurred with another page, but also happens with my very simple Index page), it throws an error saying it's unable to resolve a service for the type ILogger. I cannot find a proper way to inject this, but I also think this is not the proper way to fix it anyway.

Initially I ran into the problem of the test not being able to inject IConfiguration, but I "fixed" this by using services.AddSingleton<IConfiguration>(new ConfigurationBuilder().Build());.

I am thinking that something went wrong before that already. In the bUnit documentation, I don't see any configuration being specified, so my guess is I'm already doing something wrong there.

Since the error does not seem to contain Blazorise calls, I assume it's an issue with bUnit, not with Blazorise. If it is, please let me know so I can close this ticket here and move it to the Blazorise repository.

Example:
Testing this component:

App.razor

@using WeedIt.Web.Pages.Elements
<Blazorise.ThemeProvider Theme="@_theme">
    <Router AppAssembly="@typeof(Program).Assembly" PreferExactMatches="@true">
        <Found Context="routeData">
            <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)"/>
        </Found>
        <NotFound>
            <LayoutView Layout="@typeof(MainLayout)">
                <p>Sorry, there's nothing at this address.</p>
            </LayoutView>
        </NotFound>
    </Router>
</Blazorise.ThemeProvider>

MainLayout.razor

@inherits LayoutComponentBase

<Layout Sider="true">
    <LayoutSider>
        <LayoutSiderContent>
            <NavMenu/>
        </LayoutSiderContent>
    </LayoutSider>

    <Layout Padding="Padding.Is5">
        <LayoutContent>
            @Body
        </LayoutContent>
    </Layout>
</Layout>

Index.razor

@page "/"

<h3>My page</h3>

<Paragraph>
    Some text here
</Paragraph>

With this test:

var testContext = new TestContext();

var services = testContext.Services;
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddBlazorise(options => { options.ChangeTextOnKeyPress = true; });
services.AddBootstrapProviders();
services.AddFontAwesomeIcons();

services.AddSingleton<IConfiguration>(new ConfigurationBuilder().Build());

_renderedComponent = testContext.RenderComponent<Index>();

Results in this output:

System.InvalidOperationException
Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger`1[Microsoft.AspNetCore.Components.Server.Circuits.RemoteJSRuntime]' while attempting to activate 'Microsoft.AspNetCore.Components.Server.Circuits.RemoteJSRuntime'.
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateArgumentCallSites(Type serviceType, Type implementationType, CallSiteChain callSiteChain, ParameterInfo[] parameters, Boolean throwIfCallSiteNotFound)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateConstructorCallSite(ResultCache lifetime, Type serviceType, Type implementationType, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(ServiceDescriptor descriptor, Type serviceType, CallSiteChain callSiteChain, Int32 slot)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.TryCreateExact(Type serviceType, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.CreateCallSite(Type serviceType, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.<>c__DisplayClass7_0.<GetCallSite>b__0(Type type)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetCallSite(Type serviceType, CallSiteChain callSiteChain)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.CreateServiceAccessor(Type serviceType)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngineScope.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Bunit.Rendering.WebTestRenderer..ctor(IRenderedComponentActivator activator, IServiceProvider services, ILoggerFactory loggerFactory) in /_/src/bunit.web/Rendering/WebTestRenderer.cs:line 26
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitCache(ServiceCallSite callSite, RuntimeResolverContext context, ServiceProviderEngineScope serviceProviderEngine, RuntimeResolverLock lockType)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite singletonCallSite, RuntimeResolverContext context)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass1_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Bunit.TestServiceProvider.GetServiceInternal(Type serviceType) in /_/src/bunit.core/TestServiceProvider.cs:line 88
   at Bunit.TestServiceProvider.GetService(Type serviceType) in /_/src/bunit.core/TestServiceProvider.cs:line 80
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Bunit.TestContextBase.get_Renderer() in /_/src/bunit.core/TestContextBase.cs:line 24
   at Bunit.Extensions.TestContextBaseExtensions.RenderInsideRenderTree[TComponent](TestContextBase testContext, RenderFragment renderFragment) in /_/src/bunit.core/Extensions/TestContextBaseExtensions.cs:line 28
   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

Expected behavior:

Component Index is rendered

Version info:

  • bUnit version: 1.1.5
  • .NET Runtime and Blazor version: .NET 5.0
  • OS type and version: Windows 10 Home 64-bit (10.00, Build 19042)

Additional context:

<ItemGroup>
  <PackageReference Include="Blazorise" Version="0.9.3.3" />
  <PackageReference Include="Blazorise.Bootstrap" Version="0.9.3.3" />
  <PackageReference Include="Blazorise.Charts" Version="0.9.3.3" />
  <PackageReference Include="Blazorise.DataGrid" Version="0.9.3.3" />
  <PackageReference Include="Blazorise.Icons.FontAwesome" Version="0.9.3.3" />
  <PackageReference Include="Blazorise.Sidebar" Version="0.9.3.3" />
</ItemGroup>

I am testing my project from a separate test project (My.Project.Test testing My.Project)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions