bunit Validate NavigationManager Url on async button click

bunit Validate NavigationManager Url on async button click

我有一个有两个按钮的组件,一个是删除客户,然后 returns 到客户列表,第二个是 returns 到客户列表的取消按钮。

<td class="text-right">
    <button id="Delete_Cust" class="btn btn-primary" @onclick="Delete">Delete Customer</button>
    <button id ="Cancel_Btn" class="btn btn-secondary" @onclick="Cancel">Cancel</button>
</td>

[Parameter]
public int Id { get; set; }

protected async Task Delete()
{
    await _customer.DeleteCustomerAsync(Id); // THIS IS MOCKED AND RETURNS TRUE
    NavigationManager.NavigateTo("/customers");
}

void Cancel()
{
    NavigationManager.NavigateTo("/customers");
}

删除的代码是模拟的,returns 是真的。当我对取消按钮进行单元测试时它可以工作但是对于删除按钮它没有给我正确的 url

[Fact]
public void DeleteButtonClick()
{
    // ARRANGE
    var nav = _ctx.Services.GetRequiredService<NavigationManager>();
    var cut = _ctx.RenderComponent<CustomerDelete>(parameters =>
        parameters.Add(p => p.Id, 1));

    // ACT
    cut.Find("#Delete_Cust").Click();

    // Assert
    nav.Uri.Should().Be("http://localhost/customers"); // RETURNS http://localhost/
}

[Fact]
public void CancelClick()
{
    var nav = _ctx.Services.GetRequiredService<NavigationManager>();
    var cut = _ctx.RenderComponent<CustomerDelete>(parameters =>
        parameters.Add(p => p.Id, 1));

    cut.Find("#Cancel_Btn").Click();

    nav.Uri.Should().Be("http://localhost/customers"); // RETURNS CORRECT 
}

删除测试失败,出现以下内容

Expected nav.Uri to be "http://localhost/customers" but was "http://localhost/"

使您的 [事实] 成为 async Task 方法并 await 点击:

// ACT
//cut.Find("#Delete_Cust").Click();
await cut.Find("#Delete_Cust").ClickAsync(new MouseEventArgs());