Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to inject NavigationManager in bunit Blazor component unit test

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>());         
like image 788
Noor All Safaet Avatar asked Mar 01 '23 23:03

Noor All Safaet


2 Answers

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());
like image 141
Akinzekeel Avatar answered May 04 '23 17:05

Akinzekeel


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.

like image 22
Egil Hansen Avatar answered May 04 '23 16:05

Egil Hansen