Book Image

RSpec Essentials

By : Mani Tadayon
Book Image

RSpec Essentials

By: Mani Tadayon

Overview of this book

This book will teach you how to use RSpec to write high-value tests for real-world code. We start with the key concepts of the unit and testability, followed by hands-on exploration of key features. From the beginning, we learn how to integrate tests into the overall development process to help create high-quality code, avoiding the dangers of testing for its own sake. We build up sample applications and their corresponding tests step by step, from simple beginnings to more sophisticated versions that include databases and external web services. We devote three chapters to web applications with rich JavaScript user interfaces, building one from the ground up using behavior-driven development (BDD) and test-driven development (TDD). The code examples are detailed enough to be realistic while simple enough to be easily understood. Testing concepts, development methodologies, and engineering tradeoffs are discussed in detail as they arise. This approach is designed to foster the reader’s ability to make well-informed decisions on their own.
Table of Contents (17 chapters)
RSpec Essentials
About the Author
About the Reviewers

Controlling when hooks are executed

Both before and after accept an argument, which can be :suite, :context, or :example. The default value is :example, so when we defined the before block in our tests above, we were actually defining a before(:example) block. Similarly, our after block was actually an after(:example) block. This means that the block is executed before (or after) each test case in the context where it is defined. This is most often the desired behavior for hooks. However, there are cases when setup and teardown are not required after each test. Usually, to improve performance, in those cases you can set the hook to run before the context where it is defined, or before the entire test suite.

There is also an around block (which also accepts an argument of :suite, :context, or :example) that can be used to define a single block that does both setup and teardown. I prefer to usually keep setup and teardown separate. Also, around hooks do not have access to the test context like...