在Bunit测试时,如何从Anglesharp.Inode访问输入元素ID或名称?

发布于 2025-01-28 10:26:15 字数 2571 浏览 4 评论 0原文

我正在尝试使用Bunit测试我们的大火项目,并在Bunit 断言中遇到问题。

这是我要测试的组件:

<div>
foreach(KeyValuePair<String, String> entry in Dictionary<String, String>)
{
    <div class="form-check">
        <input class="form-check-input" type="radio" name="[email protected]" id="[email protected]@entry.Key" checked=@(firstOptionEnabled) @onclick="() => doSomethingAwesome = entry.Key">
        <label class="form-check-label" for="[email protected]@entry.Key"> @entry.Value </label>
    </div>
    firstOptionEnabled = false;
}
</div>
@code
{
    [Parameter]
    public Document ChosenDocument { get; set; }
}

我编写的测试看起来像这样:

[Fact]
public async void MyTest()
{
    var cut = RenderComponent<MyComponent ChosenDocument="documentToTest">();
    IRefreshableElementCollection<IElement> allElements = cut.FindAll("div [class=\"form-check\"]");
    // These are the <div class="form-check"> elements
    foreach(IElement element in allElements)
    {
        // These are the <input> elements
        foreach(INode node in element.ChildNodes)
        {
            // Here, I want to assert on data within each of these `INode` 
        }
    }
}

我想执行的servert正在发生我的问题。

我想断言id input> input elements之类的 so:

Assert.Equal(("myAwesomeName-" + ChosenDocument.docId), <something here ...>);

where &lt; where &lt; ;ID INPUT元素的名称。但是,我似乎无法从正在测试的组件中掌握ID name name 。

当我调试测试并在“当地人”窗口中查看时,我可以看到这一点:

- node {AngleSharp.Html.Dom.HtmlInputElement}   AngleSharp.Dom.INode {AngleSharp.Html.Dom.HtmlInputElement}
    // ...
    Id      "myAwesomeName-1234567890-text"    string
    // ...
    Name    "myAwesomeName-1234567890"         string
    // ...

因此,看来这两个信息应该在我的servert中可用,但是它们不是;好吧,至少对我来说并不明显。

如何访问我需要的这两个数据断言

I am attempting to test our Blazor project with bUnit and am having issues with a bUnit Assert.

This is the component I am trying to test:

<div>
foreach(KeyValuePair<String, String> entry in Dictionary<String, String>)
{
    <div class="form-check">
        <input class="form-check-input" type="radio" name="[email protected]" id="[email protected]@entry.Key" checked=@(firstOptionEnabled) @onclick="() => doSomethingAwesome = entry.Key">
        <label class="form-check-label" for="[email protected]@entry.Key"> @entry.Value </label>
    </div>
    firstOptionEnabled = false;
}
</div>
@code
{
    [Parameter]
    public Document ChosenDocument { get; set; }
}

The test I have written looks like this:

[Fact]
public async void MyTest()
{
    var cut = RenderComponent<MyComponent ChosenDocument="documentToTest">();
    IRefreshableElementCollection<IElement> allElements = cut.FindAll("div [class=\"form-check\"]");
    // These are the <div class="form-check"> elements
    foreach(IElement element in allElements)
    {
        // These are the <input> elements
        foreach(INode node in element.ChildNodes)
        {
            // Here, I want to assert on data within each of these `INode` 
        }
    }
}

My problem is happening with the Assert I want to perform.

I want to assert the id or name of each of the input elements like so:

Assert.Equal(("myAwesomeName-" + ChosenDocument.docId), <something here ...>);

Where <something here ...> is either id or name of the input element. However, I cannot seem to get hold of the id or name from the component under test.

When I debug the test and look in the "Locals" window, I can see this:

- node {AngleSharp.Html.Dom.HtmlInputElement}   AngleSharp.Dom.INode {AngleSharp.Html.Dom.HtmlInputElement}
    // ...
    Id      "myAwesomeName-1234567890-text"    string
    // ...
    Name    "myAwesomeName-1234567890"         string
    // ...

So, it looks like those two pieces of information should be available to me within my Assert, but they are not; well, at least not evident to me.

How can I access those two pieces of data I need for my Assertion?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

暖树树初阳… 2025-02-04 10:26:15

像这样的工作吗?您应该能够直接获取所有输入元素:

var inputs = cut.FindAll(".form-check-input");
foreach (var input in inputs)
{
    Assert.Equal($"myAwesomeName-{ChosenDocument.docId}", input.GetAttribute("name"));
}

甚至更简洁:

var inputs = cut.FindAll(".form-check-input");
Assert.All(inputs, i => Assert.Equal($"myAwesomeName-{ChosenDocument.docId}", i.GetAttribute("name")));

Does something like this work? You should be able to grab all the input elements directly:

var inputs = cut.FindAll(".form-check-input");
foreach (var input in inputs)
{
    Assert.Equal(
quot;myAwesomeName-{ChosenDocument.docId}", input.GetAttribute("name"));
}

or even more concise:

var inputs = cut.FindAll(".form-check-input");
Assert.All(inputs, i => Assert.Equal(
quot;myAwesomeName-{ChosenDocument.docId}", i.GetAttribute("name")));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文