1

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

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

删除的代码被模拟并返回true。当我对取消按钮进行单元测试时,它可以工作,但是对于删除按钮,它没有给我正确的 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 
}

删除测试失败并显示以下内容

预期 nav.Uri 为“http://localhost/customers”,但为“http://localhost/”

4

1 回答 1

1

使您的 [Fact] 成为一种async Task方法,await然后单击:

// ACT
//cut.Find("#Delete_Cust").Click();
await cut.Find("#Delete_Cust").ClickAsync(new MouseEventArgs());
于 2022-03-02T18:08:02.300 回答