Skip to Content
JavaScript 经典实例:第三版
book

JavaScript 经典实例:第三版

by Adam D. Scott, Matthew MacDonald, Shelley Powers
September 2022
Intermediate to advanced
585 pages
8h 17m
Chinese
China Electric Power Press Ltd.
Content preview from JavaScript 经典实例:第三版
284
10
0
这样的边界值和潜在的边缘情况(比如可隐性强制转换类型的字符串或
BigInt
值)。另外,还要编写测试检查可以正确处理失败情况,对这个函数来说,是
抛出预期的错误。
2.
让测试失败。
编写好测试之后,编写代码。一些
TDD
践行者建议,第一步是编
译代码,让测试失败。通过这一步,可以确保测试能够运行,测试的需求是有
意义的,而且在完成代码之前不会让测试意外通过。
3.
让测试通过。
接下来的一步可以说是“尽可能让测试通过”。也就是,你不用
担心使用的是不是最佳方案,只要能让全部测试通过即可。不要编写测试需求
以外的代码。
4.
重构。
成功让测试通过之后,开始改进代码。这一步要做的是重构、删除重复
的代码,改进之后再次运行全部测试,确保依然可以通过。如果发现没有涵盖
的情况,再编写更多测试。
TDD
的一个明显优势是,你可以把注意力集中在手头的问题上。你不需要分析设计
要求,设法编写解决方案。相反,你只需按照测试确定好的规范进行编程。另外,
TDD
在应用演进的过程中也能助你一臂之力,让你不再害怕修改。只要测试仍然可
以通过你制定好的测试,只要测试的是有代表性的情况(这一点更为重要),你就
可以放心把新版本纳入代码基。
若想得到这种防护,你需要在测试上投入更多的时间,而且需要大量经验积累才能
写出正确的测试。有一个指标可以帮助你评估测试的整体情况,即测试代码覆盖率。
10.8
跟踪测试代码覆盖率
10.8.1
问题
你想评估测试用例对所有代码情况的覆盖程度。
10.8.2
方案
让测试工具报告代码覆盖率。在
Jest ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

JavaScript Patterns

JavaScript Patterns

Stoyan Stefanov
JavaScript

JavaScript

David Flanagan
JavaScript

JavaScript

T. J. Crowder

Publisher Resources

ISBN: 9787519869601