Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

How do I mock in injected LocalStorageService is a bUnit test?

$
0
0

I'm using bUnit to test a Blazor component, but am getting stuck with trying to mock the following injected dependency in the component...

  [Inject]  public ILocalStorageService LocalStorage { get; set; } = null!;

My basic code for creating an instance of the component under test is...

TestComponentSender cut = RenderComponent<TestComponentSender>(ps => ps    .Add(p => p.NotificationHelper, new TestNotificationHelper())).Instance;

I can't use the Add method as I did with the NotificationHelper parameter, as the local storage property is injected, not a parameter.

I have tried adding the following line before the above...

Services.AddScoped<ILocalStorageService>();

...but that gives an exception when the test is run...

Cannot instantiate implementation type 'Blazored.LocalStorage.ILocalStorageService' for service type 'Blazored.LocalStorage.ILocalStorageService'

I tried referencing the NSubstitute Nuget package, and adding the following line after creating the component...

cut.LocalStorage = Substitute.For<ILocalStorageService>();

...but that gave a different exception...

Cannot provide a value for property 'LocalStorage' on type 'InspiredGiving.Crm.Areas.General.Shared.TestComponentSender'. There is no registered service of type 'Blazored.LocalStorage.ILocalStorageService'.

Anyone able to advise how I mock this injected dependency?


Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>