question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scoped IComponentActivator breaks bUnit

See original GitHub issue

I’m pretty sure the problem reported in the SO question below is due to Blazorise registering a IComponentActivator as scoped, replacing our singleton registered BunitComponentActivator. However, since the WebTestRenderer is registered as a singleton it cannot be resolved.

The fix may be to register the renderer as a scoped service and all other dependent services like this too.

https://stackoverflow.com/questions/76597158/bunit-blazorise-cannot-consume-scoped-service-microsoft-aspnetcore-components

related: #1129

Issue Analytics

  • State:closed
  • Created 3 months ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
linkdotnetcommented, Jul 2, 2023

Correct. My understanding is that a scoped service can depend on a singleton service, so I do not expect this would break users. It would solve a problem like the one being reported here, where users have to take extra steps.

Do you see any downsides to us registering all our services as scoped?

Yes, but singleton services can not depend on scoped services. Like the BunitHtmlParser that is a singleton, can not depend on TestRenderer that is a scoped service.

Anyway - making everything scoped would be my preferred anyway. Technically it wouldn’t change much the majority of use cases.

1reaction
linkdotnetcommented, Jul 2, 2023

I’ll quote the official Blazorise docs:

For testing purposes, there is currently an internal Blazorise Service that should be configured as Transient so the bUnit test engine does not throw an error. You should add this setup: ctx.Services.AddBlazorise().Replace(ServiceDescriptor.Transient<IComponentActivator, ComponentActivator>());

Other than that it should pretty much work out of the box. Let us know if you’re having any difficulties. For some testing examples, you can look at some of our <Blazorise.Link To=“https://github.com/Megabit/Blazorise/tree/master/Tests/Blazorise.Tests” Target=“Target.Blank”>tests</Blazorise.Link>.

That said, that would help the person in question on SO.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Bunit & Blazorise Cannot consume scoped service ...
Replace(ServiceDescriptor.Transient<IComponentActivator, ComponentActivator>()); Other than that it should pretty much work out of the box. Let ...
Read more >
bUnit/CHANGELOG.md at main
bUnit is a testing library for Blazor components that make tests look, feel, ... Allow using 3rd party IComponentActivator at the same time...
Read more >
Injecting services into components under test
It is common for components under test to have a dependency on services, injected into them through the @inject IMyService MyService syntax in...
Read more >
Newest 'blazorise' Questions - Stack Overflow - OpenQM
I am receiving this error to do a unit test with spec test and bunit for a project in Blazor and Blazorise: Cannot...
Read more >
[Solved]-Blazor: Cannot initialize a ComponentBase more than once ...
Inside the IComponentActivator constructor i load the registered Pages once (into a ... InvalidOperationException: Cannot consume scoped service 'Microsoft.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found