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

Why mock?

A unit in a real-world software system interfaces with many other units, internal and external. To have focused, isolated tests, we must somehow manage these interactions with other units. There are a few options. One is to simply use the other unit, as in the normal operation of the software in production. This option has the advantage that we don't do anything different in our tests, as compared to real-world usage of the software. However, this approach has two drawbacks. First, it undermines our goal of isolated unit testing by tying the tests for one unit with the exercise of code in another unit. Second, it is often not practical to load and set up the other unit in our tests due to a complex setup, external dependencies, and increased runtime of the test.

A common approach to dealing with the problem of external interfaces in testing is to use a mock in place of the other unit. Given Ruby's dynamic nature, it is quite easy to implement such a feature by using Module#define_method...