bunit 等待 OnInitializedAsync 完成后再断言字段

发布于 2025-01-11 02:10:46 字数 2272 浏览 0 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(1

跨年 2025-01-18 02:10:46

WaitForAssertion() 需要抛出异常而不是返回 false。

//component.WaitForAssertion(() => component.FindAll("td").Count.Equals(2));

应该成为

component.WaitForAssertion(() => component.FindAll("td").Count.Should().Be(2));

或(更好)使用 WaitForState()

component.WaitForState(() => component.FindAll("td").Count.Equals(2));

WaitForAssertion() needs something that throws instead of returning false.

//component.WaitForAssertion(() => component.FindAll("td").Count.Equals(2));

should become

component.WaitForAssertion(() => component.FindAll("td").Count.Should().Be(2));

or (better) use WaitForState()

component.WaitForState(() => component.FindAll("td").Count.Equals(2));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文