JavaScript事件处理程序是否应进行单元测试(Should JavaScript Event Handlers Be Unit Tested)

系统教程 行业动态 更新时间:2024-06-14 16:58:30
JavaScript事件处理程序是否应进行单元测试(Should JavaScript Event Handlers Be Unit Tested)

这里有很多关于其他语言的单元测试事件处理程序的问题,但是在JavaScript方面我找不到一个好的答案。 具体来说,我说的是一个案例:

// inside a "view" class definition: handleCheckboxClick: function() { this.relevantData.toggleSomeValue(); return false; } // later on: $('#someCheckbox').on('click', view.handleCheckboxClick);

显然,事件处理程序中存在逻辑( this.relevantData.toggleSomeValue() ),但同时没有其他方法会调用此处理程序,因此它不像单元测试那样会捕获一些未来与重构相关的错误。 在任何给定的JavaScript代码库中都有很多这样的处理程序,因此测试它们是一项非常重要的工作。

此外,在许多JS商店(当然也包括我们的商店)中,还有使用Selenium进行的功能级测试,通常会捕获明显的UI问题(例如当事件处理程序中断时)。

所以,一方面我得到了“单位测试逻辑==好”,而且我不想通过不测试某些代码来拍摄自己,如果我稍后付钱的话。 另一方面,当涉及单元测试时,这个特定的代码子集似乎具有高成本和低价值。 因此,我对SO社区的问题是,JS开发人员是否应该测试他们的事件处理函数?

There are a lot of questions here about unit test event handlers in other languages, but I haven't been able to find a good answer when it comes to JavaScript. Specifically I'm talking about a case like:

// inside a "view" class definition: handleCheckboxClick: function() { this.relevantData.toggleSomeValue(); return false; } // later on: $('#someCheckbox').on('click', view.handleCheckboxClick);

Clearly there is logic in the event handler (this.relevantData.toggleSomeValue()), but at the same time no other method will ever call this handler, so it's not like unit-testing it will catch some future refactoring-related bug. And in any given JavaScript codebase there are A LOT of these handlers, so it's a non-trivial amount of work to test them.

Plus, in many JS shops (certainly in ours) there are also feature-level tests being done with Selenium that would typically catch obvious UI issues (such as when an event handler breaks).

So, on the one hand I get that "unit testing of logic == good", and I don't want to shoot myself in the foot by not testing certain code if I will pay for it later. On the other hand this particular sub-set of code seems to have a high cost and low value when it comes to unit testing. Thus, my question for the SO community is, should JS developers unit test their event handling functions?

最满意答案

这肯定属于意见问题,但我会继续给我的:)

没有

根据我的经验,在单元级别测试这样的处理程序并不符合成本效益。 我通常最终花费大量时间站起来并引发人工事件; 这会导致通过测试,但对我的视图在现实世界中正常运行的信心不大(这也不是很有趣)。 我发现在其他地方度过的所有时间都要好得多。

但是,某种覆盖范围重要,我尝试使用以下方法实现这一目标:

将处理程序中的复杂逻辑移动到可以单独测试的辅助函数中 。 理想情况下,它们足够“纯净”,可以在单位环境中轻松测试。 如果它们不容易测试,请忘记它们! 依靠集成测试来测试与您的视图的交互 。 这是测试各种交互式场景的最简单的地方,例如“当我在textarea中输入消息并按下回车键时,会出现一条新消息”,而不会出现人为触发交互的麻烦。 不是写硒测试是我对美好时光的想法;) 放松。 覆盖率最好用测试的功能测量,而不是测试的方法/处理程序。 如果您确信您对应用程序的交互所做出的假设被“覆盖” - 以这种或那种方式 - 那么您已经完成了自己的工作。

对于所有应用程序或团队来说并非如此,但事实证明,这对我所参与的团队和应用程序来说是最有效的。 我很想听听别人对此事的看法。

This definitely falls under a matter of opinion, but I will go ahead and give mine :)

No

In my experience it is not cost effective to test handlers like this at the unit level. I usually end up spending a great deal of time standing up fixtures and triggering artificial events; this results in passing tests, but little confidence that my view will function properly in the real world (it isn't much fun either). I find that all that time is much better spent elsewhere.

Some sort of coverage is important though, and I try to achieve that with the following approach:

Move complex logic within handlers into helper functions that can be tested in isolation. Ideally these are "pure" enough that they can be easily tested in a unit environment. If they are not easy to test, forget about them! Rely on integration tests to test interactions with your views. This is the easiest place to test a wide range of interactive scenaries e.g. "when I enter a message into the textarea and press the enter key, a new message appears", without the headache of artificially triggering the interaction. Not that writing selenium tests is my idea of a fantastic time ;) Relax. Coverage is best measured in terms functionality tested, rather than methods/handlers tested. If you feel confident that the assumptions you've made about your application's interactions are "covered" — in one way or another — then you've done your job.

This is not true for all applications or teams, but it's proved to be the most effective thing for the teams and applications i've worked on. I'm interested to hear what others think on the matter.

更多推荐

本文发布于:2023-04-15 03:28:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/a312e48ac596021b97825785d9811b53.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单元测试   事件   程序   JavaScript   Tested

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!