bunit 在异步按钮单击时验证 NavigationManager Url
我有一个具有两个按钮的组件,一个用于删除客户,然后返回到客户列表,第二个用于返回到客户列表的取消按钮。
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
让您的 [Fact] 成为一个
异步任务
方法并等待
点击:Make your [Fact] an
async Task
method andawait
the Click: