bunit 等待 OnInitializedAsync 完成后再断言字段
我有一个 blazor 服务器组件,它将其客户加载到表中,它在 OnInitializedAsync 方法中获取客户。有没有办法等待 OnInitializedAsync 完成?
组件
<table>
<thead>
<tr>
<th>Name</th>
<th>Id</th>
</tr>
</thead>
<tbody>
@foreach (var customer in _customers)
{
<tr>
<td>@customer.Name</td>
<td>@customer.Id</td>
</tr>
}
</tbody>
</table>
@code {
IEnumerable<Customer> _customers = new List<Customer>();
protected override async Task OnInitializedAsync()
{
_customers = await _customerService.GetCustomersAsync();
StateHasChanged();
}
}
我需要等待测试完成才能运行我的测试。
var ctx = new TestContext();
ctx.Services.AddHttpContextAccessor();
ctx.Services.AddScoped<ICustomerService, CustomerServiceMock>();
var component = ctx.RenderComponent<Customers>();
var tableRows = component.FindAll("tr");
var buttons = component.FindAll("a");
var addButton = buttons.FirstOrDefault(b => b.OuterHtml.Contains("Add"));
component.WaitForAssertion(() => component.FindAll("td").Count.Equals(2));
var tds = component.FindAll("td");
var ths = component.FindAll("th");
// ASSERT
tableRows.Should().NotBeNull();
addButton.Should().NotBeNull();
tds.Count.Should().Be(2);
ths.Count.Should().Be(2);
ths[0].OuterHtml.Should().Be("<th>Name</th>");
ths[1].OuterHtml.Should().Be("<th>Id</th>");
tds[0].OuterHtml.Should().Be("<td>Customer XXX</td>");
tds[1].OuterHtml.Should().Be("<td>999</td>");
如果我单独运行测试,则测试运行良好,但当我一起运行所有测试时,测试会失败并出现错误
预期 tds.Count 为 2,但结果为 0。
模拟
public class CustomerServiceMock : ICustomerServiceMock
{
public async Task<IEnumerable<Customer>> GetCustomersAsync()
{
return new List<Customer>
{
await GetCustomer()
};
}
private static async Task<Customer> GetCustomer()
{
return await Task.Run(() => new Customer
{
Id = 999,
Name = "Customer XXX",
});
}
}
I have a blazor server component that load its customers into a table, it gets the customers in the OnInitializedAsync method. Is there a way to wait for the OnInitializedAsync to complete?
Component
<table>
<thead>
<tr>
<th>Name</th>
<th>Id</th>
</tr>
</thead>
<tbody>
@foreach (var customer in _customers)
{
<tr>
<td>@customer.Name</td>
<td>@customer.Id</td>
</tr>
}
</tbody>
</table>
@code {
IEnumerable<Customer> _customers = new List<Customer>();
protected override async Task OnInitializedAsync()
{
_customers = await _customerService.GetCustomersAsync();
StateHasChanged();
}
}
I need my test to wait until this finishes to run my test.
var ctx = new TestContext();
ctx.Services.AddHttpContextAccessor();
ctx.Services.AddScoped<ICustomerService, CustomerServiceMock>();
var component = ctx.RenderComponent<Customers>();
var tableRows = component.FindAll("tr");
var buttons = component.FindAll("a");
var addButton = buttons.FirstOrDefault(b => b.OuterHtml.Contains("Add"));
component.WaitForAssertion(() => component.FindAll("td").Count.Equals(2));
var tds = component.FindAll("td");
var ths = component.FindAll("th");
// ASSERT
tableRows.Should().NotBeNull();
addButton.Should().NotBeNull();
tds.Count.Should().Be(2);
ths.Count.Should().Be(2);
ths[0].OuterHtml.Should().Be("<th>Name</th>");
ths[1].OuterHtml.Should().Be("<th>Id</th>");
tds[0].OuterHtml.Should().Be("<td>Customer XXX</td>");
tds[1].OuterHtml.Should().Be("<td>999</td>");
The test runs fine if I run it individually, but fails when I run all the tests together with error
Expected tds.Count to be 2, but found 0.
Mock
public class CustomerServiceMock : ICustomerServiceMock
{
public async Task<IEnumerable<Customer>> GetCustomersAsync()
{
return new List<Customer>
{
await GetCustomer()
};
}
private static async Task<Customer> GetCustomer()
{
return await Task.Run(() => new Customer
{
Id = 999,
Name = "Customer XXX",
});
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WaitForAssertion() 需要抛出异常而不是返回 false。
应该成为
或(更好)使用 WaitForState()
WaitForAssertion() needs something that throws instead of returning false.
should become
or (better) use WaitForState()