Rails Testing Book Every Day Rails Rspec withou frontpage PDF

Title Rails Testing Book Every Day Rails Rspec withou frontpage
Author Ranjit Reddy P
Course Information Technology
Institution Pondicherry University
Pages 137
File Size 2 MB
File Type PDF
Total Downloads 31
Total Views 155

Summary

Download Rails Testing Book Every Day Rails Rspec withou frontpage PDF


Description

Everyday Rails Testing with RSpec A practical approach to test-driven development Aaron Sumner This book is for sale at http://leanpub.com/everydayrailsrspec This version was published on 2013-04-24

This is a Leanpub book. Leanpub empowers authors and publishers with the Lean Publishing process. Lean Publishing is the act of publishing an in-progress ebook using lightweight tools and many iterations to get reader feedback, pivot until you have the right book and build traction once you do. ©2012 - 2013 Aaron Sumner

Tweet This Book! Please help Aaron Sumner by spreading the word about this book on Twitter! The suggested hashtag for this book is #everydayrailsrspec. Find out what other people are saying about the book by clicking on this link to search for this hashtag on Twitter: https://twitter.com/search/#everydayrailsrspec

Contents Preface to this edition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

i

Acknowledgements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

ii

1. Introduction . . . . . . . . . . Why RSpec? . . . . . . . . . . Who should read this book . . My testing philosophy . . . . How the book is organized . . Downloading the sample code Code conventions . . . . . . . Discussion and errata . . . . . About the sample application .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1 2 2 3 3 4 5 6 6

2. Setting up RSpec . Gemfile . . . . . . Test database . . . RSpec configuration Generators . . . . . Questions . . . . . Exercises . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. 7 . 7 . 9 . 10 . 11 . 13 . 14

3. Model specs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Anatomy of a model spec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating a model spec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The new RSpec syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing validations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing class methods and scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing for failures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . DRYer specs with describe, context, before and after . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

15 15 16 18 19 22 23 24 25 29 30 30

CONTENTS

4. Generating test data with factories . . . Factories versus fixtures . . . . . . . . . Adding factories to the application . . . . Simplifying our syntax . . . . . . . . . . Associations and inheritance in factories Generating better fake data . . . . . . . . Advanced associations . . . . . . . . . . How to abuse factories . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . Exercises . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

31 31 33 35 36 39 40 41 42 42

5. Basic controller specs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Why test controllers? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Controller testing basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Setting up test data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing GET requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing POST requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing PUT requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing DESTROY requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing non-CRUD methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing nested routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Testing non-HTML controller output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

44 44 45 46 47 48 50 52 53 54 55 55 58 58

6. Advanced controller specs . . . Setting up the spec . . . . . . . Testing the guest role . . . . . . Testing the admin and user roles Summary . . . . . . . . . . . . Exercise . . . . . . . . . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

59 59 63 66 72 73

7. Controller spec cleanup . . . Shared examples . . . . . . . . Creating helper macros . . . . Using custom RSpec matchers Summary . . . . . . . . . . . Exercises . . . . . . . . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . .

. . . . . . . . . . . . . . . . . . . . . . . .

74 74 80 82 83 83

8. Feature specs . . . . . . Why feature specs? . . . Additional dependencies A basic feature spec . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . . . . . . . .

. . . .

. . . .

. 85 . 85 . 86 . 87

CONTENTS

Changes in Capybara 2.0 . . . . Adding feature specs . . . . . . Debugging feature specs . . . . A little refactoring . . . . . . . . Including JavaScript interactions Capybara drivers . . . . . . . . Summary . . . . . . . . . . . . Exercises . . . . . . . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

89 89 90 91 92 96 97 97

9. Speeding up specs . . . . . . . . . . Optional terse syntax . . . . . . . . Mocks and stubs . . . . . . . . . . . Automation with Guard and Spork Tags . . . . . . . . . . . . . . . . . Other speedy solutions . . . . . . . Summary . . . . . . . . . . . . . . Exercises . . . . . . . . . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

98 98 102 104 106 107 108 108

10. Testing the rest . . . Testing email delivery . Testing file uploads . . Testing the time . . . . Testing web services . Testing rake tasks . . . Summary . . . . . . . Exercises . . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

109 109 111 112 113 114 115 116

11. Toward test-driven development Practice testing the small things . . Be aware of what you’re doing . . . Short spikes are OK . . . . . . . . . Write a little, test a little is also OK Strive to write request specs first . . Keep practicing . . . . . . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

117 117 117 117 118 118 118

12. Parting advice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Make time for testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Keep it simple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Don’t revert to old habits! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Use your tests to make your code better . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sell others on the benefits of automated testing . . . . . . . . . . . . . . . . . . . . . . . . Goodbye, for now . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

119 119 119 119 119 120 120

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . . .

. . . . . . .

. . . . . . . .

. . . . . . .

. . . . . . . .

. . . . . . .

. . . . . . . .

. . . . . . .

. . . . . . . .

. . . . . . .

. . . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

. . . . . . .

More testing resources for Rails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121

CONTENTS

RSpec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 Rails testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122 About Everyday Rails . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 About the author . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 Colophon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 Change log . . . . . . April 15, 2013 . . . March 9, 2013 . . . February 20, 2013 . February 13, 2013 . December 11, 2012 November 29, 2012 August 3, 2012 . . . July 3, 2012 . . . . June 1, 2012 . . . . May 25, 2012 . . . . May 18, 2012 . . . . May 11, 2012 . . . . May 7, 2012 . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

127 127 127 127 127 127 128 128 128 128 128 129 129 129

Preface to this edition This is it, the final update built on Rails 3.2. As soon as I press the publish button on Leanpub I’m going to get to work on a major update for Rails 4.0. I wanted to let you know about a few things before we dig into testing with RSpec: First, I’m happy to share that I’ve open sourced the sample code and posted it to GitHub. You can now browse the source there, or grab it for yourself and follow along each chapter’s tagged branch. Along those lines, I’m moving errata tracking and code discussions away from my own site and the Leanpub product page over to GitHub. It’s better for sharing code and will hopefully involve other readers. You may find a bug or typo or something else that doesn’t make sense. I still find things I want to change, even after working on this book for more than a year. If you find something, please post it in the issues on the GitHub project. I can’t promise it will be fixed in the 3.2 version, but if it makes sense I’ll be sure to address it in the Rails 4.0 edition. Otherwise, the biggest changes in this version are in chapters 8, 9, and 10. For the most part these aren’t huge changes–mostly an attempt to bring old content up-to-speed with the current codebase. They’ll be works in progress moving forward. Finally, I want to talk a bit about what I’ve got in mind for the next version. It hopefully goes without saying, but the sample application will be built on Rails 4.0. It’ll also be a new application! Over the past year I’ve come to realize that our little address book is limited in terms of what I can demonstrate with it, especially when it comes to feature specs. I don’t know what I’ll replace it with just yet, but whatever it is will hopefully be more flexible. There will be other changes throughout, culminating in a chapter of actually test-driving a new feature instead of adding after-the-fact specs. Of course, these updates will continue to be free to everyone who’s purchased the book. So thanks for reading–hope you like this edition, and I hope to hear from you soon on GitHub, Twitter or email. Aaron

i

Acknowledgements First, thank you to why the lucky stiff¹ for introducing me to Ruby through his weird, fun projects and books. The Ruby community just isn’t the same without him. Thanks to all the other great minds in the Ruby community I haven’t met for making me a better developer–even if it doesn’t always show in my code. Thanks to the readers of the Everyday Rails blog for providing good feedback on my original series of RSpec posts, and helping me realize they might make for a decent book. Thanks to everyone who purchased an early copy of the book–the response it’s received has been incredible, and your feedback has helped tremendously. Thanks to David Gnojek for critiquing the dozen or so covers I designed for the book and helping me pick a good one. Check out Dave’s work in art and design at DESIGNOJEK². Thanks to...


Similar Free PDFs