Bunit为什么告诉我它不能“单击”。一个按钮是因为该元素没有onclick的事件处理程序,即使它确实如此?
这是它在其.razor
页面中的元素:
<button id="@(theID)" type="button" class="btn btn-primary" onclick="window.open('https://www.google.com/');">Do Something</button>
我在Bunit test .cs文件中有此语句:
IElement theButton = cut.Find("button[id=\"" + theID + "\"]");
exportButton.MarkupMatches("<button id=\"" + theID + "\" type=\"button\" class=\"btn btn-primary\" onclick=\"window.open('https://www.google.com/');\">Do Something</button>");
exportButton.Click();
Bunit通过exportbutton.markupmatches()
测试,因此是标记Bunit收到的内容:
<button id="abc123" type="button" class="btn btn-primary" onclick="window.open('https://www.google.com/');">Do Something</button>
但是,Bunit失败的是click> click()
。错误消息是这样:
My.Domain.Tests.TestTheThing
Source: MyAwesomeTest.razor line N
Duration: 485 ms
Message:
Bunit.MissingEventHandlerException : The element does not have an event handler for the event 'onclick', nor any other events.
Stack Trace:
TriggerEventDispatchExtensions.TriggerBubblingEventAsync(ITestRenderer renderer, IElement element, String eventName, EventArgs eventArgs) line 102
TriggerEventDispatchExtensions.TriggerEventAsync(IElement element, String eventName, EventArgs eventArgs) line 76
MouseEventDispatchExtensions.ClickAsync(IElement element, MouseEventArgs eventArgs) line 373
MouseEventDispatchExtensions.Click(IElement element, Int64 detail, Double screenX, Double screenY, Double clientX, Double clientY, Double offsetX, Double offsetY, Int64 button, Int64 buttons, Boolean ctrlKey, Boolean shiftKey, Boolean altKey, Boolean metaKey, String type) line 322
MyAwesomeTest.TestTheThing() line N
<>c.<ThrowAsync>b__140_0(Object state)
正如标记Bunit接收到的那样可以清楚地看到:
<button id="abc123" type="button" class="btn btn-primary" onclick="window.open('https://www.google.com/');">Do Something</button>
确实有一个onclick
要处理的事件。
那么为什么测试失败呢?为什么布尼特说即使存在的事件也不可操作。
This is the element as it looks in its .RAZOR
page:
<button id="@(theID)" type="button" class="btn btn-primary" onclick="window.open('https://www.google.com/');">Do Something</button>
I have this statement in the bUnit test .CS file:
IElement theButton = cut.Find("button[id=\"" + theID + "\"]");
exportButton.MarkupMatches("<button id=\"" + theID + "\" type=\"button\" class=\"btn btn-primary\" onclick=\"window.open('https://www.google.com/');\">Do Something</button>");
exportButton.Click();
bUnit passes the exportButton.MarkupMatches()
test as this is what is in the markup bUnit receives:
<button id="abc123" type="button" class="btn btn-primary" onclick="window.open('https://www.google.com/');">Do Something</button>
However, it's the Click()
that bUnit is failing on. The error message is this:
My.Domain.Tests.TestTheThing
Source: MyAwesomeTest.razor line N
Duration: 485 ms
Message:
Bunit.MissingEventHandlerException : The element does not have an event handler for the event 'onclick', nor any other events.
Stack Trace:
TriggerEventDispatchExtensions.TriggerBubblingEventAsync(ITestRenderer renderer, IElement element, String eventName, EventArgs eventArgs) line 102
TriggerEventDispatchExtensions.TriggerEventAsync(IElement element, String eventName, EventArgs eventArgs) line 76
MouseEventDispatchExtensions.ClickAsync(IElement element, MouseEventArgs eventArgs) line 373
MouseEventDispatchExtensions.Click(IElement element, Int64 detail, Double screenX, Double screenY, Double clientX, Double clientY, Double offsetX, Double offsetY, Int64 button, Int64 buttons, Boolean ctrlKey, Boolean shiftKey, Boolean altKey, Boolean metaKey, String type) line 322
MyAwesomeTest.TestTheThing() line N
<>c.<ThrowAsync>b__140_0(Object state)
As can be clearly seen in the markup bUnit receives:
<button id="abc123" type="button" class="btn btn-primary" onclick="window.open('https://www.google.com/');">Do Something</button>
There is, indeed an onclick
event to handle.
So why is the test failing? Why is bUnit saying there is not a handleable event even though there is.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不是与C#事件处理程序绑定,而是对JavaScript的处理程序。 Bunit只能触发C#事件处理程序,也就是。以
@
(例如@onclick
)开头的那些。这是因为Bunit不运行JavaScript,因此您不能使用它在HTML中使用它来测试在线JavaScript。将您的JavaScript放入.js文件中的函数中,然后将其调用。然后,您可以使用BUNIT的JSINTEROP实现来验证调用该功能。
有关此信息的更多信息: https://bunit.doc.dev/docs/test-docs/test-docs/test-docs/test-dock- /emulating-ijsruntime.html
You are not binding to a C# event handler, but to a JavaScript one. bUnit can only trigger C# event handlers, aka. the ones that start with an
@
, e.g.@onclick
.This is because bUnit does not run JavaScript, so you cannot use it to test online JavaScript in your HTML like that. Put your JavaScript into a function in .js file, and Jsinterop to call it. Then you can use bUnit's implementation of JSInterop to verify that the function was called.
More about that here: https://bunit.dev/docs/test-doubles/emulating-ijsruntime.html