简体   繁体   English

bUnit 找不到与给定选择器匹配的元素

[英]bUnit can not find element that matches given selecter

Using .net 6, bUnit 1.13.5 Mudblazor 6.0.17使用 .net 6、bUnit 1.13.5 Mudblazor 6.0.17

I am trying to write unit tests for a razor component that is in a dialog box and bUnit is unable to find any of the components or class/ids in it.我正在尝试为对话框中的剃刀组件编写单元测试,但 bUnit 无法在其中找到任何组件或类/id。 I am getting the following error...我收到以下错误...

Message: Bunit.ElementNotFoundException: No elements were found that matches the selector '#CancelButton'消息:Bunit.ElementNotFoundException:找不到与选择器“#CancelButton”匹配的元素

in the ColumnTrackerEditDialog.razor there is the following line在 ColumnTrackerEditDialog.razor 中有以下行

        <MudButton id="CancelButton" OnClick="@Cancel">Cancel</MudButton>

This should always be rendered and it not under any conditional statements.这应该始终呈现,而不是在任何条件语句下。 The unit test for this is as follows (so far)对此的单元测试如下(到目前为止)

        [Test]
        public async Task ColumnTrackerEditDialogUITest_CancelButton()
        {
            var item = GetColumnTrackerPeco();

            PopulateData(item);

            var page = TestContext!.RenderComponent<ColumnTrackerEditDialog>(parameters =>
                parameters.Add(p => p.Item, item));
            Assert.IsNotNull(page);

            var cancelButton = page.Find("#CancelButton");
            Assert.IsNotNull(cancelButton);
            cancelButton.Click();
        }

The test set up is fine as the page is not null and on non dialog pages I can find elements by ids, classes or components.测试设置很好,因为页面不为空,并且在非对话框页面上我可以通过 id、类或组件找到元素。

Is this an issue because it's a dialog or am I missing something else?这是一个问题,因为它是一个对话还是我错过了其他东西?

I have tried to find "button", ".mud-button" and other classes, as well as.FindComponent and other tags within the component and it's not finding anything.我试图在组件中找到“按钮”、“.mud-button”和其他类,以及 .FindComponent 和其他标签,但没有找到任何东西。

Is the component being added asynchronously?组件是否被异步添加? If that's the case, try using cut.WaitForElement("#CancelButton") instead.如果是这种情况,请尝试改用cut.WaitForElement("#CancelButton")

Another approach is to stub out the MudBlazor component, so it's not directly influences your test, and instead find the stub and assert you passed the expected parameters to it.另一种方法是将 MudBlazor 组件存根,这样它就不会直接影响您的测试,而是找到存根并断言您已将预期参数传递给它。 Learn more here: https://bunit.dev/docs/providing-input/substituting-components在此处了解更多信息: https ://bunit.dev/docs/providing-input/substituting-components

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM