ebook img

RSpec Essentials: Develop testable, modular, and maintainable Ruby software for the real world using RSpec PDF

223 Pages·2016·4.789 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview RSpec Essentials: Develop testable, modular, and maintainable Ruby software for the real world using RSpec

www.it-ebooks.info RSpec Essentials Develop testable, modular, and maintainable Ruby software for the real world using RSpec Mani Tadayon BIRMINGHAM - MUMBAI www.it-ebooks.info RSpec Essentials Copyright © 2016 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: April 2016 Production reference: 1130416 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78439-590-2 www.packtpub.com www.it-ebooks.info Credits Author Project Coordinator Mani Tadayon Judie Jose Reviewers Proofreader Stefan Daschek Safis Editing Nola Stowe Indexer Rekha Nair Commissioning Editor Amarabha Banerjee Graphics Abhinash Sahu Acquisition Editor Reshma Raman Production Coordinator Manu Joseph Content Development Editor Rashmi Suvarna Cover Work Manu Joseph Technical Editor Anushree Arun Tendulkar Copy Editor Safis Editing www.it-ebooks.info About the Author Mani Tadayon first learned to program as a shy 7th grader on an Apple IIe using BASIC. He went on to learn Pascal, C++, Java, JavaScript, Visual Basic, and PHP before becoming a Ruby developer in 2008. With 15 years of experience in the software industry, he has developed expertise in web development, infrastructure, and testing. Mani's interests and education are broad, with degrees in foreign languages, computer science, and geography. He lives just far enough from Silicon Valley with his family and their many, many Shiba Inus. www.it-ebooks.info About the Reviewers Stefan Daschek started hacking on computers back in the days of the Commodore 64/128. Since then, he has studied computer sciences at TU Vienna and founded his own company, Büro DIE ANTWORT. Currently, he is mainly developing and maintaining complex web applications written in Ruby on Rails, and sometimes soldering stuff and building small robots. His original Commodore 128 is still working and used for a collective session of Summer Games every so often. Nola Stowe has been programming with Ruby since Rails 0.8 and testing with RSpec since its early days. She is an independent consultant, helping to augment teams of Ruby and Clojure developers. She has also been a technical reviewer for Instant RSpec Test-Driven Development, Packt Publishing and The Rails Way, First Edition. Nola blogs at http://blog.rubygeek.com and http://www.clojuregeek.com. Thanks to my husband Nick for doing the mundane things in life to allow me time do what I love doing. You are the greatest! www.it-ebooks.info www.PacktPub.com eBooks, discount offers, and more Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. TM https://www2.packtpub.com/books/subscription/packtlib Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can search, access, and read Packt's entire library of books. Why subscribe? • Fully searchable across every book published by Packt • Copy and paste, print, and bookmark content • On demand and accessible via a web browser www.it-ebooks.info Table of Contents Preface v Chapter 1: Exploring Testability from Unit Tests to Behavior-Driven Development 1 The promise of testing 1 Testability 3 Technical assumptions 4 Running our first spec 5 Understanding the unit test 9 Writing specs with RSpec 12 Test-driven development 14 Behavior-driven development 15 Summary 16 Chapter 2: Specifying Behavior with Examples and Matchers 17 Structure of a spec file 17 Using let and context 20 Matchers 23 Built-in matchers 23 Custom matchers 24 Enhanced context in matcher output 26 Creating a good custom error message 29 Improving application code 31 Testing for errors 35 Summary 40 [ i ] www.it-ebooks.info Table of Contents Chapter 3: Taking Control of State with Doubles and Hooks 41 Why mock? 41 Mocks, stubs, and doubles 45 Using hooks 46 Controlling when hooks are executed 52 Advanced state control with hooks and mocks 53 Summary 58 Chapter 4: Setting Up and Cleaning Up 59 Configuring RSpec with spec_helper.rb 59 Initialization and configuration of resources 62 A case study of test simplicity with an external service 63 A case study of production faithfulness with a test resource instance 76 Summary 83 Chapter 5: Simulating External Services 85 The importance of external web services 85 Mock HTTP responses with custom helpers 86 Using real-world requests for mock responses 87 Using WebMock for mock HTTP requests 93 Using VCR for mock requests 94 Summary 98 Chapter 6: Driving a Web Browser with Capybara 99 Getting started with Capybara and Selenium 100 Integrating Capybara with RSpec 103 Why Capybara? 106 Black-box JavaScript tests with a real browser 107 Summary 108 Chapter 7: Building an App from the Outside In with Behavior-Driven Development 109 Exploring BDD 110 MMFs by example 111 Using TodoMVC 113 Specifying the MMF 114 Configuring RSpec to run a feature file 116 The BDD process begins 119 Building web apps with Rack 120 Serving static files using Rack middleware 123 Summary 125 [ ii ] www.it-ebooks.info Table of Contents Chapter 8: Tackling the Challenges of End-to-end Testing 127 Step 1 – building the view 127 Step 2 – defining the API 133 Step 3 – finishing the view 157 The pros and cons of BDD 160 Simple Rack authentication middleware 161 JSON Web Token 164 Summary 166 Chapter 9: Configurability 167 Configuration and testability 168 What is so important about configuration? 168 What does all this have to do with testing? 169 File-based configuration management 169 A better configuration manager 173 Summary 184 Chapter 10: Odds and Ends 185 Reducing duplication with shared example groups 185 Mocking time 193 Detecting false negatives and false positives 196 Testing mixins with dummy containers 200 Summary 202 Index 203 [ iii ] www.it-ebooks.info

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.