- Mastering React Test:Driven Development
- Daniel Irvine
- 173字
- 2021-06-24 14:45:09
Checking for null or not
There is a school of thought that checking for null in tests is redundant and can be removed all together. In other words, the second-to-last test we wrote would become this:
it('renders a label for the first name field', () => {
render();
expect(labelFor('firstName').textContent).toEqual('First name');
});
In the original test, the first not null expectation is a guard expectation. The test will still function even without this line, since the second expectation will throw an exception if the value of label is null.
The run-time difference between the two variants of the test is subtle. The original test will fail with a test failure, not an exception. In this second version, your test will fail with an exception, not a test failure.
In some languages and test environments, exceptions can be more painful than test failures. Ultimately, which style you choose is a matter of personal preference. In this book, I'll continue to use guard expectations for completeness, but I'd encourage you to experiment with both styles.
- Python快樂編程:人工智能深度學習基礎
- 軟件測試項目實戰之性能測試篇
- Git高手之路
- Cassandra Design Patterns(Second Edition)
- GitLab Repository Management
- 面向對象程序設計(Java版)
- 劍指MySQL:架構、調優與運維
- Bootstrap for Rails
- C++從入門到精通(第6版)
- Oracle Database XE 11gR2 Jump Start Guide
- 大話代碼架構:項目實戰版
- Cinder:Begin Creative Coding
- Programming MapReduce with Scalding
- 移動智能系統測試原理與實踐
- R for Data Science Cookbook