bunit 在异步按钮单击时验证 NavigationManager Url

发布于 2025-01-11 04:10:32 字数 1559 浏览 0 评论 0原文

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

<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/”

I have a component that has two buttons, one to delete a customer and then returns to the customer list and the second a cancel button that returns to a list of customers.

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

The code to the delete is mocked and returns true. When I unit test the cancel button it works but for the Delete button it does not give me the correct 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 
}

The Delete Test fails with the following

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

洋洋洒洒 2025-01-18 04:10:32

让您的 [Fact] 成为一个异步任务方法并等待点击:

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

Make your [Fact] an async Task method and await the Click:

// ACT
//cut.Find("#Delete_Cust").Click();
await cut.Find("#Delete_Cust").ClickAsync(new MouseEventArgs());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文