如何在 bunit Blazor 组件单元测试中注入 NavigationManager

How to inject NavigationManager in bunit Blazor component unit test

从单元测试注入时出现此错误。

System.InvalidOperationException: ''NavigationManagerProxy' has not been initialized.'

我的代码:

Services.AddSingleton<NavigationManager>(Mock.Of<NavigationManager>());         

我用这个 class 进行单元测试:

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);
    }
}

然后在测试里面这样设置:

Services.AddSingleton<NavigationManager>(new TestNav());

在为 Blazing Pizza Workshop 编写测试时,我们使用了这个(类似于@Akinzekeel 的代码):

    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));
        }
    }

测试看起来像这样:

@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");
    }
}

我没有测试过各种场景,但这似乎至少在这种情况下有效。

bUnit fakes NavigationManager automatically 并将其注册到测试上下文服务,以便验证导航,您可以获取 bUnit 伪造并检查 url:

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

或者如果你想触发导航:

navMan.NavigateTo("newUrl");