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
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Mocks, stubs, and doubles


The word mock is generic. Often the words stub, fake, and double are used to mean the same thing. To be precise, we could make a distinction between mocks and stubs. A mock is a fake object that stands in the place of an actual object. A stub is a fake method that is executed in place of an actual method. In practice, the terms are used interchangeably. In fact, our Object#mock method should have been named Object#stub if we were being consistent.

RSpec uses the method double to handle mocking. This is aliased as stub and mock, which can be confusing, but these aliases are both deprecated, so you shouldn't use them. There is also a method called spy, which is a convenience method based on double that I encourage you to avoid. All of these are defined in rspec-mocks, RSpec's test double framework.

You can think of double as returning a dummy object. For very simple cases, we could simply use Object.new instead. For example, in our shopping cart test, we didn't do...