Effective Testing with RSpec 3
Build Ruby Apps with Confidence
by Myron Marston, Ian Dees
Version: P1.0 (August 2017)
Copyright 2017 The Pragmatic Programmers, LLC. This book is licensed to the individual who purchased it. We don't copy-protect it because that would limit your ability to use it for your own purposes. Please don't break this trustyou can use this across all of your devices but please do not share this copy with other members of your team, with friends, or via file sharing services. Thanks.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf and the linking g device are trademarks of The Pragmatic Programmers, LLC.
Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.
About the Pragmatic Bookshelf
The Pragmatic Bookshelf is an agile publishing company. Were here because we want to improve the lives of developers. We do this by creating timely, practical titles, written by programmers for programmers.
Our Pragmatic courses, workshops, and other products can help you and your team create better software and have more fun. For more information, as well as the latest Pragmatic titles, please visit us at http://pragprog.com.
Our ebooks do not contain any Digital Restrictions Management, and have always been DRM-free. We pioneered the beta book concept, where you can purchase and read a book while its still being written, and provide feedback to the author to help make a better book for everyone. Free resources for all purchasers include source code downloads (if applicable), errata and discussion forums, all available on the book's home page at pragprog.com. Were here to make your life easier.
New Book Announcements
Want to keep up on our latest titles and announcements, and occasional special offers? Just create an account on pragprog.com (an email address and a password is all it takes) and select the checkbox to receive newsletters. You can also follow us on twitter as @pragprog.
About Ebook Formats
If you buy directly from pragprog.com, you get ebooks in all available formats for one price. You can synch your ebooks amongst all your devices (including iPhone/iPad, Android, laptops, etc.) via Dropbox. You get free updates for the life of the edition. And, of course, you can always come back and re-download your books when needed. Ebooks bought from the Amazon Kindle store are subject to Amazon's polices. Limitations in Amazon's file format may cause ebooks to display differently on different devices. For more information, please see our FAQ at pragprog.com/frequently-asked-questions/ebooks. To learn more about this book and access the free resources, go to https://pragprog.com/book/rspec3, the book's homepage.
Thanks for your continued support,
Andy Hunt
The Pragmatic Programmers
The team that produced this book includes: Andy Hunt (Publisher) Janet Furlow (VP of Operations) Susannah Davidson Pfalzer (Executive Editor) Jacquelyn Carter (Development Editor) Potomac Indexing, LLC (Indexing) Liz Welch (Copy Editor) Gilson Graphics (Layout)
For customer support, please contact .
For international rights, please contact .
Table of Contents
Copyright 2017, The Pragmatic Bookshelf.
Early praise for Effective Testing with RSpec 3
Ruby embraces the old Lisp idea that you should build up a language to address your problem, and RSpec carries this approach into the world of testing. But RSpec is a big toolbox, and in order to harness its full power you need a guide. This is that guide: the bridge you need to take you from writing tests, to expressing your design requirements in code.
Avdi Grimm |
Author of Confident Ruby and Exceptional Ruby and Head Chef, RubyTapas |
Effective Testing with RSpec 3 does a great job of explaining both the main features of RSpec as well as its lesser known, powerful, and often overlooked features. Its an essential resource for Rubyists looking to learn RSpec, or for those who use it every day. Ive already started using it regularly as a resource.
Noel Rappin |
Author of Rails 4 Test Prescriptions and Director of Development, Table XI |
Myron and Ian have written the essential missing manual for modern RSpec, connecting all the latest best practices for TDD and BDD. If you were having trouble mastering modern testing with RSpec, this book will sort you out.
Sam Joseph |
Co-instructor on the BerkeleyX Agile Development using Ruby on Rails MOOC and co-founder, AgileVentures Charity |
Myron has been the driving force behind RSpec development for years. He is an expert on testing practices and getting the most out of the RSpec suite. His care and attention to detail are impeccable, and it shows in both the codebase and this book.
Xavier Shay |
RSpec core team member and Payments Engineering Lead, Square |
Having worked with Myron for many years on the RSpec core team, Im very pleased to see Effective Testing with RSpec 3 . This book contains everything you need to learn not only how RSpec works, but how to test effectively with the framework. If youre working with RSpec on a daily basis and looking to get better at working with the framework, I could not recommend this book more heartily.
Sam Phippen |
RSpec core team member and engineer, Digital Ocean |
The exercises are illustrative and challenging. The final chapter on test doubles really synthesizes the best way to use these tools. Youll write far more readable and robust specs after reading this book, especially when interfacing with the shifting sands of third-party APIs.
Nigel Lowry |
Company Director and Principal Consultant, Lemmata |
Effective Testing with RSpec 3 is well worth a read: it is much more than a technical reference and will make you a better developer by teaching you how to write more expressive, robust, and maintainable tests.
Alessandro Bahgat |
Professional software developer |
Foreword
Making software is hard, and its difficult to know how to get better at it. Theres always so much stuff to learn, and so many competing opinions about the right way to do things. And yet somehow here you are, about to read another book full of ideas youre apparently supposed to remember.
Well, the truth is that you can make software just fine without writing tests, and you can write tests just fine without using RSpec, so youll be okay if you stop reading now. But if you decide to keep going, youll discover something interesting: writing tests with RSpec is a great way to get really good at making software.
Thats because RSpec isnt just a testing framework. Its a tool for learning how to think critically, patiently, and systematically about the design of your code, and how to make software in a methodical way so you have confidence that its well organized, clear, and correct.