0

我试图在我的测试中呈现“组件 A”,它里面有以下 html:

@if (functions == null)
    {
        <p><em>Loading...</em></p>
    }
    else
    {
       <p>loaded</p>
    }

'functions' 是在 CS 文件中为组件创建的变量,如下所示:

private List<FunctionViewModel> functions;

并像这样填充:

protected override async Task OnInitializedAsync()
        {
            try
            {
                await ResetView();
            }
            catch (Exception ex)
            {

            }
        }

private async Task ResetView()
        {
            functions = await GetFunctions(Id);
        }

现在,当我在测试中渲染我的组件时,我会进行标记匹配:

component.WaitForAssertion(() => component.MarkupMatches("<p><em>Loading...</em></p>",
            "Loading markup was no loaded within 10 seconds."), new TimeSpan(0, 0, 10));

这通过了,但随后我进行了相同类型的检查,但对于加载“函数”变量时应该出现的“加载”标记,但它总是失败。

谁能解释我如何加载这个组件并一路检查它的标记?

编辑:

完整的测试用例 -

[Test]
    public void LoadTitleDivOnMainPage()
    {
        //Act
        IRenderedComponent<UI> component = SetupTestContext.RenderUI(context);

        //Assert
        component.WaitForAssertion(() => component.MarkupMatches("<p><em>Loading...</em></p>",
            "Loading markup was no loaded within 10 seconds."), new TimeSpan(0, 0, 10));

        component.WaitForAssertion(() => component.MarkupMatches("<p>loaded</p>",
            "Loaded markup was no loaded within 10 seconds."), new TimeSpan(0, 0, 10));
        
    }

渲染 UI 组件的方法 -

public static IRenderedComponent<UI> RenderUI(TestContext context)
    {

        return context.RenderComponent<UI>();
    }
4

0 回答 0