在Bunit测试时,如何从Anglesharp.Inode访问输入元素ID或名称?
我正在尝试使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
像这样的工作吗?您应该能够直接获取所有输入元素:
甚至更简洁:
Does something like this work? You should be able to grab all the input elements directly:
or even more concise: