How to inject NavigationManager in bunit Blazor component unit test

5k views Asked by At

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>());         
3

There are 3 answers

1
Akinzekeel On BEST ANSWER

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());
0
Phil On

bUnit fakes NavigationManager automatically and registers it with the test context services so to verify navigation you can fetch the bUnit fake and check the url:

var navMan = ctx.Services.GetRequiredService<FakeNavigationManager>();
Assert.Equal("http://localhost/foo", navMan.Uri);

or if you want to trigger navigation:

navMan.NavigateTo("newUrl");
0
Egil Hansen On

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.