I am getting this error when injecting from unit test.
System.InvalidOperationException: ''NavigationManagerProxy' has not been initialized.'
My code:
Services.AddSingleton<NavigationManager>(Mock.Of<NavigationManager>());
I use this class for my unit tests:
internal class TestNav : NavigationManager
{
public TestNav()
{
Initialize("https://unit-test.example/", "https://unit-test.example/");
}
protected override void NavigateToCore(string uri, bool forceLoad)
{
NotifyLocationChanged(false);
}
}
Then inside the test set it up like this:
Services.AddSingleton<NavigationManager>(new TestNav());
While writing tests for the Blazing Pizza Workshop, we used this (similar to @Akinzekeel's code):
public class FakeNavigationManager : NavigationManager
{
private readonly ITestRenderer renderer;
public FakeNavigationManager(ITestRenderer renderer)
{
Initialize("http://localhost/", "http://localhost/");
this.renderer = renderer;
}
protected override void NavigateToCore(string uri, bool forceLoad)
{
Uri = ToAbsoluteUri(uri).ToString();
renderer.Dispatcher.InvokeAsync(
() => NotifyLocationChanged(isInterceptedLink: false));
}
}
and the tests look like this:
@inherits TestContext
@code {
private NavigationManager navigationManager;
public MainLayoutTests()
{
Services.AddSingleton<NavigationManager, FakeNavigationManager>();
navigationManager = Services.GetService<NavigationManager>();
}
[Fact]
public void MyOrdersNavigationItem_Is_Marked_As_Active_When_Viewing_MyOrdersPage()
{
var cut = Render(@<MainLayout />);
navigationManager.NavigateTo("myorders");
cut.Find("a[href=myorders]")
.ClassList
.Should()
.Contain("active");
}
}
I haven't tested all sorts of scenarios, but this seems to work in this case at least.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With