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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 Why RSpec? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 Who should read this book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 My testing philosophy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 How the book is organized . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 Downloading the sample code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 Code conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 Discussion and errata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 About the sample application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 2. Setting up RSpec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Gemfile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 Test database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 RSpec configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 Generators . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 Questions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 3. Model specs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Anatomy of a model spec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 Creating a model spec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 The new RSpec syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 Testing validations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 Testing instance methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 Testing class methods and scopes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 Testing for failures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 DRYer specs with describe, context, before and after . . . . . . . . . . . . . . . . . . . . . 25 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 Question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 CONTENTS 4. Generating test data with factories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Factories versus fixtures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 Adding factories to the application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 Simplifying our syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35 Associations and inheritance in factories . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 Generating better fake data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 Advanced associations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 How to abuse factories . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 5. Basic controller specs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Why test controllers? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44 Controller testing basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45 Organization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 Setting up test data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47 Testing GET requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 Testing POST requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 Testing PUT requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 Testing DESTROY requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 Testing non-CRUD methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 Testing nested routes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Testing non-HTML controller output . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 6. Advanced controller specs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Setting up the spec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 59 Testing the guest role . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 Testing the admin and user roles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72 Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 7. Controller spec cleanup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Shared examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 Creating helper macros . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 Using custom RSpec matchers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 8. Feature specs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Why feature specs? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 Additional dependencies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 A basic feature spec . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 CONTENTS Changes in Capybara 2.0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Adding feature specs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 Debugging feature specs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 A little refactoring . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 Including JavaScript interactions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 Capybara drivers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 9. Speeding up specs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Optional terse syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 Mocks and stubs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102 Automation with Guard and Spork . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 Tags . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 106 Other speedy solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108 10. Testing the rest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Testing email delivery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 Testing file uploads . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Testing the time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 Testing web services . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 Testing rake tasks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 11. Toward test-driven development . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Practice testing the small things . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Be aware of what youre doing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Short spikes are OK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117 Write a little, test a little is also OK . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Strive to write request specs first . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 Keep practicing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 12. Parting advice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Make time for testing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Keep it simple . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Dont revert to old habits! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Use your tests to make your code better . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 Sell others on the benefits of automated testing . . . . . . . . . . . . . . . . . . . . . . . . 120 Goodbye, for now . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 April 15, 2013 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 March 9, 2013 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 February 20, 2013 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 February 13, 2013 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 December 11, 2012 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 November 29, 2012 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 August 3, 2012 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 July 3, 2012 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 June 1, 2012 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 May 25, 2012 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128 May 18, 2012 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 May 11, 2012 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 May 7, 2012 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 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 Im 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, Im happy to share that Ive 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 chapters tagged branch. Along those lines, Im moving errata tracking and code discussions away from my own site and the Leanpub product page over to GitHub. Its better for sharing code and will hopefully involve other readers. You may find a bug or typo or something else that doesnt 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 cant promise it will be fixed in the 3.2 version, but if it makes sense Ill 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 arent huge changesmostly an attempt to bring old content up-to-speed with the current codebase. Theyll be works in progress moving forward. Finally, I want to talk a bit about what Ive got in mind for the next version. It hopefully goes without saying, but the sample application will be built on Rails 4.0. Itll also be a new application! Over the past year Ive 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 dont know what Ill 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 whos purchased the book. So thanks for readinghope 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 isnt the same without him. Thanks to all the other great minds in the Ruby community I havent met for making me a better developereven if it doesnt 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 bookthe response its 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 Daves work in art and design at DESIGNOJEK. Thanks to family and friends who wished me the best for this project, even though they had no idea what I was talking about. And finally, thank you to my wife for putting up with my obsession with making new things, even when it keeps me up way too late or awake all night. And thanks to the cats for keeping me company while doing so. http://en.wikipedia.org/wiki/Why_the_lucky_stiff http://www.designojek.com/ ii 1. Introduction Ruby on Rails and automated testing go hand in hand. Rails ships with a built-in test framework; if its not to your liking you can replace it with one of your liking (as I write this, Ruby Toolbox lists 16 projects under the Unit Test Frameworks category alone). So yeah, testings pretty important in Railsyet many people developing in Rails are either not testing their projects at all, or at best only adding a few token specs on model validations. In my opinion, there are several reasons for this. Perhaps working with Ruby or web frameworks is a novel enough concept; adding an extra layer of work seems like just thatextra work. Or maybe there is a perceived time constraintspending time on writing tests takes time away from writing the features our clients or bosses demand. Or maybe the habit of defining test as the practice of clicking links in the browser is just too hard to break. Ive been there. I dont consider myself an engineer in the traditional sense, yet I have problems to solve and typically find solutions in building software. Ive been developing web applications since 1995, but usually as a solo developer on shoestring public sector projects. Aside from some exposure to BASIC as a kid, a little C++ in college, and a wasted week of Java training in my second grown-up job outside of college, Ive never had any honest-to-goodness schooling in software development. In fact, it wasnt until 2005, when Id had enough of hacking ugly spaghetti-style PHP code, that I sought out a better way to write web applications. Id looked at Ruby before, but never had a serious use for it until Rails began gaining steam. There was a lot to learna new language, an actual architecture, and a more object-oriented approach (despite what you may think about Rails treatment of object orientation, its far more object oriented than anything I wrote in my pre-framework days). Even with all those new challenges, though, I was able to create complex applications in a fraction of the time it took me in my previous framework-less efforts. I was hooked. That said, early Rails books and tutorials focused more on speed (build a blog in 15 minutes!) than on good practices like testing. If testing were covered at all, it was generally reserved for a chapter toward the end. Newer works on Rails have addressed this shortcoming, and now demonstrate how to test applications throughout. A number of books have been written specifically on the topic of testing. But without a sound approach to the testing side, many developersespecially those in a similar boat to the one I was inmay find themselves without a consistent testing strategy. My goal with this book is to introduce you to a consistent strategy that works for meone that you can then adapt to make work consistently for you, too. https://www.ruby-toolbox.com/categories/testing_frameworks http://en.wikipedia.org/wiki/Spaghetti_code 1 1. Introduction 2 Why RSpec? Nothing against the other test frameworks out there, but for whatever reason RSpec is the one thats stuck with me. Maybe it stems from my backgrounds in copywriting and software development, but for me RSpecs capacity for specs that are readable without being cumbersome is a winner. Ill talk more about this later in the book, but Ive found that with a little coaching even most non-technical people can read a spec written in RSpec and understand whats going on. Who should read this book If Rails is your first foray into a web application framework, and your past programming experience didnt involve any testing to speak of, this book will hopefully help you get started. If youre really new to Rails, you may find it beneficial to review coverage of development and basic testing in the likes of Michael Hartls Rails 3 Tutorial or Sam Rubys Agile Web Development with Rails (4th Edition) before digging into Everyday Rails Testing with RSpecthis book assumes youve got some basic Rails skills under your belt. In other words, this book wont teach you how to use Rails, and it wont provide a ground-up introduction to the testing tools built into the frameworkwere going to be installing a few extras to make the testing process easier to comprehend and manage. If youve been developing in Rails for a little while, and maybe even have an application or two in production but testing is still a foreign conceptthis book is for you! I was in your shoes for a long time, and the techniques Ill share here helped me improve my test coverage and think more like a test-driven developer. I hope theyll do the same for you. Specifically, you should probably have a grasp of MVC architecture, as used in Rails Bundler How to run rake tasks Basic command line techniques On the more advanced end, if youre familiar with using Test::Unit, MiniTest, or even RSpec itself, and already have a workflow in place that (a) youre comfortable with and (b) provides adequate coverage, you may be able to fine-tune some of your approach to testing your applicationsbut to be honest, at this point youre probably on board with automated testing and dont need this extra nudge. This is not a book on testing theory; it also wont dig too deeply into performance issues. Other books may be of more use to you in the long run. Refer to More Testing Resources for Rails at the end of this book for links to these and other books, websites, and testing tutorials. 1. Introduction 3 My testing philosophy Discussing the right way to test your Rails application can invoke holy warsnot quite as bad as, say, the Vim versus Emacs debate, but still not something to bring up in an otherwise pleasant conversation with fellow Rubyists. Yes, there is a right way to do testingbut if you ask me there are degrees of right when it comes to testing. At the risk of starting riots among the Ruby test-driven/behavior-driven development communities, my approach focuses on the following foundation: Tests should be reliable. Tests should be easy to write. Tests should be easy to understand. If you mind these three factors in your approach, youll go a long way toward having a sound test suite for your applicationnot to mention becoming an honest-to-goodness practitioner of Test- Driven Development. Yes, there are some tradeoffsin particular: Were not focusing on speed (though we will talk about it later). Were not focusing on overly DRY code in our tests (and well talk about this, too). In the end, though, the most important thing is that youll have testsand reliable, understandable tests, even if theyre not quite as optimized as they could be, are a great way to start. Its the approach that finally got me over the hump between writing a lot of application code, calling a round of browser-clicking testing, and hoping for the best; versus taking advantage of a fully automated test suite and using tests to drive development and ferret out potential bugs and edge cases. And thats the approach well take in this book. How the book is organized In Everyday Rails Testing with RSpec Ill walk you through taking a basic Rails 3.2 application from completely untested to respectably tested with RSpec. The book is organized into the following activities: Youre reading chapter 1, Introduction, now. In chapter 2, Setting Up RSpec, well set up a new or existing Rails application to use RSpec, along with a few extra, useful testing tools. 1. Introduction 4 In chapter 3, Model Specs, well tackle testing our applications models through reliable unit testing. Chapter 4, Factories, covers factories, making test data generation straightforward. Well take an initial look at testing controllers in chapter 5, Basic Controller Specs. Chapter 6, Advanced Controller Specs, is about using controller specs to make sure your authentication and authorization layers are doing their jobsthat is, keeping your apps data safe. Chapter 7, Controller Spec Cleanup, is our first round of spec refactoring, reducing redundancy without removing readability. In chapter 8, Integration Testing with Feature Specs, well move on to integration testing with request specs, thus testing how the different parts of our application interact with one another. In chapter 9, Speeding up specs, well go over some techniques for refactoring and running your tests with performance in mind. Chapter 10, Testing the Rest, covers testing those parts of our code we havent covered yetthings like email, file uploads, and time-specific functionality. Ill talk about what it means to practice test-driven development in chapter 11, Toward Test- driven Development. Finally, well wrap things up in chapter 12, Parting Advice. Each chapter contains the step-by-step guide process I used to get better at testing my own software. Many chapters conclude with a question-and-answer section, followed by a few exercises to follow when using these techniques on your own. Again, I strongly recommend working through the exercises in your own applicationswe wont be building an application together in this book, just exploring code patterns and techniques. Downloading the sample code Speaking of the sample code, you can find a completely tested application on GitHub. Get the source! https://github.com/ruralocity/everyday_rails_rspec_rails32 If youre familiar with Git (and, as a Rails developer, you should be), you can clone the source to your computer. Each chapters work has its own branch. Grab each chapters code to follow along. Branches are labeled by chapter number, but Ill also tell you which branch to check out at the start of that chapter. 1. Introduction 5 If youre not familiar with Git, you may still download the sample code a given chapter. To begin, open the project on GitHub. Then, locate the branch selector and select that chapters branch: Finally, click the ZIP download button to save the source to your computer: Git Immersion is an excellent, hands-on way to learn the basics of Git. Code conventions Im using the following setup for this application: Rails 3.2: As far as I know the techniques Im using will apply to any version of Rails from 3.0 onward. Your mileage may vary with some of the code samples. Ruby 1.9: Again, the basic techniques should work; youll just need to be mindful that Ill be using the Ruby 1.9 hash syntax and adjust accordingly if your application uses Ruby 1.8. (In other words, switch out the key: value syntax with :key => value.) RSpec 2.13: I began writing this book while version 2.8 was current, so anything you see here should apply to versions of RSpec at least that far back. 1. Introduction 6 Again, this book is not a traditional tutorial! The code provided here isnt intended to walk you through building an application; rather, its here to help you understand and learn testing patterns and habits to apply to your own Rails applications. In other words, you can copy and paste, but its probably not going to do you a lot of good. You may be familiar with this technique from Zed Shaws Learn Code the Hard Way seriesEveryday Rails Testing with RSpec is not in that exact style, but I do agree with Zed that typing things yourself as opposed to copying-and-pasting from the interwebs or an ebook is a better way to learn. Discussion and errata Nobodys perfect, especially not me. Ive put a lot of time and effort into making sure Everyday Rails Testing with RSpec is as error-free as possible, but you may find something Ive missed. If thats the case, head on over to the issues section for the source on GitHub to share an error or ask for more details. About the sample application Our sample application is an admittedly simple, admittedly ugly little contacts manager, perhaps part of a corporate website. The application lists names, email addresses, and phone numbers to anyone who comes across the site, and also provides a simple, first-letter search function. Users must log in to add new contacts or make changes to existing ones. Finally, users must have an administrator ability to add new users to the system. Up to this point, though, Ive been intentionally lazy and only used Rails default generators to create the entire application (see the 01_untested branch of the sample code). This means I have a test directory full of untouched test files and fixtures. I could run rake test at this point, and perhaps some of these tests would even pass. But since this is a book about RSpec, a better solution will be to dump this folder, tell Rails to use RSpec instead, and build out a more respectable test suite. Thats what well walk through in this book. First things first: We need to configure the application to recognize and use RSpec and to start generating the appropriate specs (and a few other useful files) whenever we employ a Rails generator to add code to the application. Lets get started. http://learncodethehardway.org/ https://github.com/ruralocity/everyday_rails_rspec_rails_3_2/issues 2. Setting up RSpec As I mentioned in chapter 1, our contacts manager is currently functioning. At least we think its functioningour only proof of that is we clicked through the links, made a few dummy accounts, and added and edited data. Of course, this doesnt scale as we add features. (Ive deployed apps with even less tests than that; I bet some of you have, too.) Before we go any further toward adding new features to the application, we need to stop what were doing and add an automated test suite to it, using RSpec and a few helper gems to make it happen. Before we dive into those specs, though, we need to do some configuring. Once upon a time, RSpec and Rails took some coaxing to get to work together. Thats not really the case anymore, but well still need to install a few things and tweak some configurations before we write any specs. In this chapter, well complete the following tasks: Well start by using Bundler to install RSpec and other gems useful in testing. Well check for a test database and install one, if necessary. Next well configure RSpec to test what we want to test. Finally, well configure a Rails application to automatically generate files for testing as we add new features. Check out the 02_setup branch of the sample source to follow along with the code samples. Using the command line, type git checkout -b 02_setup origin/02_setup. See chapter 1 for additional details. Gemfile First things first: Since RSpec isnt included in a default Rails application, well need to take a moment to install it and a few helpers. Well use Bundler to add these dependencies. Lets open our Gemfile and add the following code: 7 2. Setting up RSpec 8 Gemfile 1 group :development, :test do 2 gem "rspec-rails", "~> 2.13.0" 3 gem "factory_girl_rails", "~> 4.2.1" 4 end 5 6 group :test do 7 gem "faker", "~> 1.1.2" 8 gem "capybara", "~> 2.0.2" 9 gem "database_cleaner", "~> 0.9.1" 10 gem "launchy", "~> 2.2.0" 11 end These are the current versions of each gem as of this writing. Of course, any and all may update frequently, so keep tabs on them on Rubygems.org, GitHub, and your favorite Ruby news feeds. You need to know Bundler If this previous code sample is confusing, please set aside this book and find a tutorial on Bundler. Any Rails tutorial covering version 3.0 or newer will have content on Bundler. If youre a Railscasts subscriber, check out the revised version of the Bundler tutorial available there. As with pretty much every other aspect of Rails development these days, well be using Bundler heavily in this book. Why install in two separate groups? rspec-rails and factory_girl_rails are used in both the development and test environments. Specif- ically, they are used in development by generators well be utilizing shortly. The remaining gems are only used when you actually run your specs, so theyre not necessary to load in development. This also ensures that gems used solely for generating code or running tests arent installed in your production environment when you deploy to your server. Run bundle install from your command line to install the gems onto your system. So what did we just install? rspec-rails includes RSpec itself in a wrapper to add some extra Rails-specific features. 2. Setting up RSpec 9 factory_girl_rails replaces Rails default fixtures for feeding test data to the test suite with much more preferable factories. faker generates names, email addresses, and other placeholders for factories. capybara makes it easy to programatically simulate your users interactions with your application. database_cleaner helps make sure each spec run in RSpec begins with a clean slate, byyou guessed itcleaning data from the test database. launchy does one thing, but does it well: It opens your default web browser on demand to show you what your application is rendering. Ill cover each of these in more detail in future chapters, but in the meantime our application has access to all the basic supports necessary to build a solid test suite. Next up: Creating our test database. A note about Factory Girl versions 2012 Was a busy year for Factory Girl. Version 3.0, released in March, introduced a Ruby 1.9-only requirement. If youre stuck using Ruby 1.8, youll need to include version 1.7.0 of factory_- girl_rails, which will load the previous version. In other words, include gem "factory_girl_- rails", "> 1.7.0" in your Gemfile. Test database If youre adding specs to an existing Rails application, theres a chance youve already got a test database on your computer. If not, heres how to add one. Open the file config/database.yml to see which databases your application is ready to talk to. If you havent made any changes to the file, you should see something like the following if youre using SQLite: config/database.yml 1 test: 2 adapter: sqlite3 3 database: db/test.sqlite3 4 pool: 5 5 timeout: 5000 Or this if youre using MySQL: 2. Setting up RSpec 10 config/database.yml 1 test: 2 adapter: mysql2 3 encoding: utf8 4 reconnect: false 5 database: contacts_test 6 pool: 5 7 username: root 8 password: 9 socket: /tmp/mysql.sock Or this if youre using PostgreSQL: config/database.yml 1 test: 2 adapter: postgresql 3 encoding: utf8 4 database: contacts_test 5 pool: 5 6 username: root # or your system username 7 password: If not, add the necessary code to config/database.yml now, replacing contacts_test with the appropriate name for your application. Finally, to ensure theres a database to talk to, run the following rake task: $ bundle exec rake db:create:all If you didnt yet have a test database, you do now. If you did, the rake task politely informs you that the database already existsno need to worry about accidentally deleting a previous database. Now lets configure RSpec itself. RSpec configuration Now we can add a spec folder to our application and add some basic RSpec configuration. Well install RSpec with the following command line directive: 2. Setting up RSpec 11 $ bundle exec rails generate rspec:install As the generator dutifully reports, weve now got a configuration file for RSpec (.rspec), a directory for our spec files as we create them (spec), and a helper file where well further customize how RSpec will interact with our code (spec/spec_helper.rb). Nextand this is optionalI like to change RSpecs output from the default format to the easy-to-read documentation format. This makes it easier to see which specs are passing and which are failing; it also provides an attractive outline of your specs foryou guessed itdocumentation purposes. Open .rspec and add the following line: .rspec 1 --format documentation One last (optional) setup step: Telling Rails to generate spec files for us. Generators Thanks to the beauty of Railties, just by loading the rspec-rails and factory_girl_rails gems were all set. Rails stock generators will no longer generate the default Test::Unit files in test; theyll generate RSpec files in spec (and factories in spec/factoriesmore on factories in the next chapter). However, if youd like you can manually specify settings for these generators. If you use the scaffold generator to add code to your application, you may want to consider thisthe default generator adds a lot of specs we wont cover in this book; in particular, view specs. Open config/application.rb and include the following code inside the Application class: config/application.rb 1 config.generators do |g| 2 g.test_framework :rspec, 3 fixtures: true, 4 view_specs: false, 5 helper_specs: false, 6 routing_specs: false, 7 controller_specs: true, 8 request_specs: false 9 g.fixture_replacement :factory_girl, dir: "spec/factories" 10 end http://api.rubyonrails.org/classes/Rails/Railtie.html 2. Setting up RSpec 12 Can you guess what this code is doing? Heres a rundown: fixtures: true specifies to generate a fixture for each model (using a Factory Girl factory, instead of an actual fixture) view_specs: false says to skip generating viewspecs. I wont cover themin this book; instead well use request specs to test interface elements. helper_specs: false skips generating specs for the helper files Rails generates with each controller. As your comfort level with RSpec improves, consider changing this option to true and testing these files. routing_specs: false omits a spec file for your config/routes.rb file. If your application is simple, as the one in this book will be, youre probably safe skipping these specs. As your application grows, however, and takes on more complex routing, its a good idea to incorporate routing specs. request_specs: false skips RSpecs defaults for adding integration-level specs in spec/re- quests. Well cover this in chapter 8, at which time well just create our own files. And finally, g.fixture_replacement :factory_girl tells Rails to generate factories instead of fixtures, and to save them in the spec/factories directory. Dont forget, just because RSpec wont be generating these files for you doesnt mean you cant add them by hand. For example, if you need to add a helper spec, just add it inside spec/helpers, following the spec file naming convention. So if we wanted to test app/helpers/contacts_helper.rb, wed add spec/helpers/contacts_helper_spec.rb. If we wanted to test a hypothetical library in lib/my_library.rb wed add a spec file spec/lib/my_library_spec.rb. And so on. One last step: Even though weve got a test database, it doesnt have a schema. To make it match the development schema, run the following rake task: rake db:test:clone This clones the database structure as used in development to the test database. However, the task doesnt copy any dataany data setup that a given test requires will be up to you, as well see throughout this book. 2. Setting up RSpec 13 ABC: Always Be Cloning Dont forget, any time you use rake db:migrate to make a change to your development database, youll need to mirror that change in your test database with rake db:test:clone. If you run RSpec and get an error about an unknown database, its probably because you havent cloned yet. You can chain the two rake tasks together on your command line with rake db:migrate db:test:clone, or you can go one step further and create a shell alias with a shortcut. For example, I use the shortcut rmigc to run a migration and clone the database with a single command. With that, our application is now configured to test with RSpec! We can even give it a first run: $ be rspec No examples found. Finished in 0.00013 seconds 0 examples, 0 failures Looks good! In the next chapter well start using it to actually test the applications functionality, starting with its model layer. Questions Can I delete my test folder? If youre starting a new application from scratch, yes. If youve been developing your application for awhile, first run rake test to verify that there arent any tests contained within the directory that you may want to transfer to RSpecs spec folder. Why dont you test views? Creating reliable view tests is a hassle. Maintaining them is even worse. As I mentioned when I set up my generators to crank out spec files, I relegate testing UI-related code to my integration tests. This is a pretty standard practice among Rails developers. 2. Setting up RSpec 14 Exercises If youre working from an existing code base: Add RSpec and the other required gems to your Gemfile, and use Bundler to install. The code and techniques provided in this book will work with Rails 3.0 and newer. Make sure your application is properly configured to talk to your test database. Create your test database, if necessary. Go ahead and configure Rails generator command to use RSpec and Factory Girl for any new application code you may add moving forward. You can also just use the default settings provided by the gems. Make a list of things you need to test in your application as it now exists. This can include mission-critical functionality, bugs youve had to track down and fix in the past, new features that broke existing features, or edge cases to test the bounds of your application. Well cover these scenarios in the coming chapters. If youre working from a new, pristine code base: Follow the instructions for installing RSpec and associates with Bundler. Your database.yml file should already be configured to use a test database. If youre using a database besides SQLite youll probably need to create the actual database, if you havent already, with bundle exec rake db:create:all. Optionally, configure Rails generators to use RSpec and Factory Girl, so that as you add new models and controllers to your application youll automatically be given starter files for your specs and factories. Extra credit: OK, Im not actually handing out points for any of thisbut if you create a lot of new Rails applications, you can create a Rails application template to automatically add RSpec and related configuration to your Gemfile and application config files, not to mention create your test database. Rails Wizard and App Scrolls are great starting points for building application templates of your favorite tools. http://railswizard.org/ http://appscrolls.org 3. Model specs Weve got all the tools we need for building a solid, reliable test suitenow its time to put them to work. Well get started with the apps core building blocksits models. In this chapter, well complete the following tasks: First well create a model spec for an existing modelin our case, the actual Contact model. Then, well write passing tests for a models validations, class, and instance methods, and organize our spec in the process. Well create our first spec files and factories for existing models by hand. If and when we add new models to the application, the handy RSpec generators we configured in chapter 2 will generate placeholder files for us. Check out the 03_models branch of the sample source to follow along with the code samples. Using the command line, type git checkout -b 03_models origin/02_models. See chapter 1 for additional details. Anatomy of a model spec I think its easiest to learn testing at the model level because doing so allows you to examine and test the core building blocks of an application. (An object-oriented application without objects isnt very useful, after all.) Well-tested code at this level is keya solid foundation is the first step toward a reliable overall code base. To get started, a model spec should include tests for the following: The models create method, when passed valid attributes, should be valid. Data that fail validations should not be valid. Class and instance methods perform as expected. This is a good time to look at the basic structure of an RSpec model spec. I find it helpful to think of them as individual outlines. For example, lets look at our main Contact models requirements: 15 3. Model specs 16 describe Contact do it "is valid with a firstname, lastname and email" it "is invalid without a firstname" it "is invalid without a lastname" it "is invalid without an email address" it "is invalid with a duplicate email address" it "returns a contact's full name as a string" Well expand this outline in a few minutes, but this gives us quite a bit for starters. Its a simple spec for an admittedly simple model, but points to our first three best practices: It describes a set of expectationsin this case, what the Contact model should look like. Each example (a line beginning with it) only expects one thing. Notice that Im testing the firstname, lastname, and email validations separately. This way, if an example fails, I know its because of that specific validation, and dont have to dig through RSpecs output for cluesat least, not as deeply. Each example is explicit. The descriptive string after it is technically optional in RSpec; however, omitting it makes your specs more difficult to read. Each examples description begins with a verb, not should. Read the expectations aloud: Contact is invalid without a firstname, Contact is invalid without a lastname, Contact returns a contacts full name as a string. Readability is important! With these best practices in mind, lets build a spec for the Contact model. Creating a model spec First, well open up the spec directory and, if necessary, create a subdirectory named models. Inside that subdirectory lets create a file named contact_spec.rb and add the following: spec/models/contact_spec.rb 1 require 'spec_helper' 2 3 describe Contact do 4 it "is valid with a firstname, lastname and email" 5 it "is invalid without a firstname" 6 it "is invalid without a lastname" 7 it "is invalid without an email address" 8 it "is invalid with a duplicate email address" 9 it "returns a contact's full name as a string" 10 end 3. Model specs 17 Notice the require 'spec_helper' at the top, and get used to typing itall of your specs will include this line moving forward. Location, location, location The name and location for your spec file is important! RSpecs file structure mirrors that of the app directory, as do the files within it. In the case of model specs, contact_spec.rb should correspond to contact.rb. This becomes more important later when we start automating things. Well fill in the details in a moment, but if we ran the specs right now from the command line (using bundle exec rspec) the output would be similar to the following: Contact is invalid without a firstname (PENDING: Not yet implemented) returns a contact's full name as a string (PENDING: Not yet implemented) is invalid with a duplicate email address (PENDING: Not yet implemented) is invalid without an email address (PENDING: Not yet implemented) is invalid without a lastname (PENDING: Not yet implemented) is valid with a firstname, lastname and email (PENDING: Not yet implemented) Pending: Contact is invalid without a firstname # Not yet implemented # ./spec/models/contact_spec.rb:5 Contact returns a contact's full name as a string # Not yet implemented # ./spec/models/contact_spec.rb:9 Contact is invalid with a duplicate email address # Not yet implemented # ./spec/models/contact_spec.rb:8 Contact is invalid without an email address # Not yet implemented # ./spec/models/contact_spec.rb:7 Contact is invalid without a lastname # Not yet implemented # ./spec/models/contact_spec.rb:6 Contact is valid with a firstname, lastname and email # Not yet implemented # ./spec/models/contact_spec.rb:4 3. Model specs 18 Finished in 0.01026 seconds 6 examples, 0 failures, 6 pending Randomized with seed 15689 Great! Six pending specslets write them and make them pass, starting with the first example. Before that, though, we need to look at a recent, important change to the RSpec DSL. As we add additional models to the contacts manager, assuming we use Rails model or scaffold generator to do so, the model spec file (along with an associated factory) will be added automatically. (If it doesnt go back and configure your applications generators now, or make sure youve properly installed the rspec-rails gem, as shown in chapter 2.) The new RSpec syntax In June, 2012a few days after I released the first complete version of this bookthe RSpec team announced a new alternative to the traditional should, added to version 2.11. This new approach alleviates some technical issues caused by the old should syntax. Instead of saying something should or should_not match expected output, you expect something to or to_not be something else. As an example, lets look at this sample expectation. In Ruby, true should always, well, be true. In the old RSpec syntax, this would be written like this: it "is true when true" do true.should be_true end The new syntax passes the test value into an expect() method, then chains a matcher to it: it "is true when true" do expect(true).to be_true end http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax 3. Model specs 19 Even though many RSpec tutorials still use the old should syntax, Ive decided to use the new expect() format here. Itll help you build the muscle memory now instead of laterand its possible that should will require additional configuration in future versions of RSpec. So what does that syntax look like in a real example? Lets fill out that first expectation from our spec for the Contact model: spec/models/contact_spec.rb 1 require 'spec_helper' 2 3 describe Contact do 4 it "is valid with a firstname, lastname and email" do 5 contact = Contact.new( 6 firstname: 'Aaron', 7 lastname: 'Sumner', 8 email: '[email protected]') 9 expect(contact).to be_valid 10 end 11 12 # remaining examples to come 13 end This simple example uses RSpecs be_valid matcher to verify that our model knows what it has to look like to be valid. We set up an object (in this case, a new-but-unsaved instance of Contact called contact), then pass that to expect to compare to the matcher. Now, if we run RSpec from the command line again (via bundle exec rspec) we see one passing example! Were on our way. Now lets get into testing more of our code. If RSpec reports that it could not find table 'contacts', remember to run the rake db:test:clone task to add your applications tables to the test database. Testing validations Validations are a good way to break into automated testing. These tests can usually be written in just a line or two of code, especially when we leverage the convenience of factories. Lets look at some detail to our firstname validation spec: 3. Model specs 20 spec/models/contact_spec.rb 1 it "is invalid without a firstname" do 2 expect(Contact.new(firstname: nil)).to have(1).errors_on(:firstname) 3 end This time, we expect that the new contact (with a :firstname explicitly set to nil) will not be valid, thus returning an error on :firstname. And when we run RSpec again, we should be up to two passing specs. Now we can use the same approach to test the :lastname validation. spec/models/contact_spec.rb 1 it "is invalid without a lastname" do 2 expect(Contact.new(lastname: nil)).to have(1).errors_on(:lastname) 3 end You may be thinking that these tests are relatively pointlesshow hard is it to make sure validations are included in a model? The truth is, they can be easier to omit than you might imagine. More importantly, though, if you think about what validations your model should have while writing tests (ideally, and eventually, in a Test-Driven Development pattern), you are more likely to remember to include them. Testing that email addresses must be unique is fairly simple as well: spec/models/contact_spec.rb 1 it "is invalid with a duplicate email address" do 2 Contact.create( 3 firstname: 'Joe', lastname: 'Tester', 4 email: '[email protected]') 5 contact = Contact.new( 6 firstname: 'Jane', lastname: 'Tester', 7 email: '[email protected]') 8 expect(contact).to have(1).errors_on(:email) 9 end Notice a subtle difference here: In this case, we persisted a contact (using create instead of new) to test against, then created a second contact as the subject of the actual test. This, of course, requires 3. Model specs 21 that the persisted contact is valid (with both a first and last name) and has an email address assigned to it. In future chapters well look at utilities to streamline this process. Now lets test a more complex validation. Say we want to make sure we dont duplicate a phone number for a usertheir home, office, and mobile phones should all be unique to them. How might you test that? In the Phone model spec, we have the following example: spec/models/phone_spec.rb 1 require 'spec_helper' 2 3 describe Phone do 4 it "does not allow duplicate phone numbers per contact" do 5 contact = Contact.create(firstname: 'Joe', lastname: 'Tester', 6 email: '[email protected]') 7 home_phone = contact.phones.create(phone_type: 'home', 8 phone: '785-555-1234') 9 mobile_phone = contact.phones.build(phone_type: 'mobile', 10 phone: '785-555-1234') 11 12 expect(mobile_phone).to have(1).errors_on(:phone) 13 end 14 15 it "allows two contacts to share a phone number" do 16 contact = Contact.create(firstname: 'Joe', lastname: 'Tester', 17 email: '[email protected]') 18 contact.phones.create(phone_type: 'home', 19 phone: '785-555-1234') 20 other_contact = Contact.new 21 other_phone = other_contact.phones.build(phone_type: 22 'home', phone: '785-555-1234') 23 24 expect(other_phone).to be_valid 25 end 26 end This time, since the Contact and Phone models are coupled via an Active Record relationship, we need to provide a little extra information. In the case of the first example, weve got a contact to which both phones are assigned. In the second, the same phone number is assigned to two unique contacts. Note that, in both examples, we have to create the contact, or persist it in the database, in order to assign it to the phones were testing. 3. Model specs 22 And since the Phone model has the following validation: app/models/phone.rb validates :phone, uniqueness: { scope: :contact_id } These specs will pass without issue. Of course, validations can be more complicated than just requiring a specific scope. Yours might involve a complex regular expression or a custom method. Get in the habit of testing these validationsnot just the happy paths where everything is valid, but also error conditions. For instance, in the examples weve created so far, we tested what happens when an object is initialized with nil values. Testing instance methods It would be convenient to only have to refer to @contact.name to render our contacts full names instead of creating the string every time, so weve got this method in the Contact class: spec/models/contact.rb 1 def name 2 [firstname, lastname].join(' ') 3 end We can use the same basic techniques we used for our validation examples to create a passing example of this feature: spec/models/contact_spec.rb 1 it "returns a contact's full name as a string" do 2 contact = Contact.new(firstname: 'John', lastname: 'Doe', 3 email: '[email protected]') 4 expect(contact.name).to eq 'John Doe' 5 end Create test data, then tell RSpec how you expect it to behave. Easy, right? Lets keep going. 3. Model specs 23 Testing class methods and scopes Now lets test the Contact models ability to return a list of contacts whose names begin with a given letter. For example, if I click S then I should get Smith, Sumner, and so on, but not Jones. There are a number of ways I could implement thisfor demonstration purposes Ill show one. The model implements this functionality in the following simple method: app/models/contact.rb 1 def self.by_letter(letter) 2 where("lastname LIKE ?", "#{letter}%").order(:lastname) 3 end To test this, lets add the following to our Contact spec: spec/models/contact_spec.rb 1 require 'spec_helper' 2 3 describe Contact do 4 5 # validation examples omitted ... 6 7 it "returns a sorted array of results that match" do 8 smith = Contact.create(firstname: 'John', lastname: 'Smith', 9 email: '[email protected]') 10 jones = Contact.create(firstname: 'Tim', lastname: 'Jones', 11 email: '[email protected]') 12 johnson = Contact.create(firstname: 'John', lastname: 'Johnson', 13 email: '[email protected]') 14 15 expect(Contact.by_letter("J")).to eq [johnson, jones] 16 end 17 end Note were testing both the results of the query and the sort order; jones will be retrieved from the database first but since were sorting by last name then johnson should be stored first in the query results. 3. Model specs 24 Testing for failures Weve tested the happy patha user selects a name for which we can return resultsbut what about occasions when a selected letter returns no results? Wed better test that, too. The following spec should do it: spec/models/contact_spec.rb 1 require 'spec_helper' 2 3 describe Contact do 4 5 # validation examples ... 6 7 it "returns a sorted array of results that match" do 8 smith = Contact.create(firstname: 'John', lastname: 'Smith', 9 email: '[email protected]') 10 jones = Contact.create(firstname: 'Tim', lastname: 'Jones', 11 email: '[email protected]') 12 johnson = Contact.create(firstname: 'John', lastname: 'Johnson', 13 email: '[email protected]') 14 15 expect(Contact.by_letter("J")).to_not include smith 16 end 17 end This spec uses RSpecs include matcher to determine if the array returned by Contact.by_- letter("J")and it passes! Were testing not just for ideal resultsthe user selects a letter with resultsbut also for letters with no results. A complete list of RSpecs default matchers may be found in the README for the rspec-matchers project on GitHub. In chapter 7, well take a look at creating custom matchers of our own. If youre following along with the sample code, youve no doubt spotted a discrepancy there with what weve covered herein that code, Im using yet another RSpec feature, before, to help simplify my code and reduce typing. 3. Model specs 25 DRYer specs with describe, context, before and after Like I said, the previous code samples have some redundancy: We create the same three objects in each example. Just as in your application code, the DRY principle applies to your tests (with some exceptions; see below). Lets use a few RSpec tricks to clean things up. The first thing Im going to do is create a describe block within my describe Contact block to focus on the filter feature. The general outline will look like this: spec/models/contact_spec.rb 1 require 'spec_helper' 2 3 describe Contact do 4 5 # validation examples ... 6 7 describe "filter last name by letter" do 8 # filtering examples ... 9 end 10 end Lets break things down further by including a couple of context blocksone for matching letters, one for non-matching: spec/models/contact_spec.rb 1 require 'spec_helper' 2 3 describe Contact do 4 5 # validation examples ... 6 7 describe "filter last name by letter" do 8 context "matching letters" do 9 # matching examples ... 10 end 11 12 context "non-matching letters" do 13 # non-matching examples ... 14 end 3. Model specs 26 15 end 16 end While describe and context are technically interchangeable, I prefer to use them like thisspecifically, describe outlines a function of my class; context outlines a specific state. In my case, I have a state of a letter with matching results selected, and a state with a non-matching letter selected. As you may be able to spot, were creating an outline of examples here to help us sort similar examples together. This makes for a more readable spec. Now lets finish cleaning up our reorganized spec with the help of a before hook: spec/models/contact_spec.rb 1 require 'spec_helper' 2 3 describe Contact do 4 5 # validation examples ... 6 7 describe "filter last name by letter" do 8 before :each do 9 @smith = Contact.create(firstname: 'John', lastname: 'Smith', 10 email: '[email protected]') 11 @jones = Contact.create(firstname: 'Tim', lastname: 'Jones', 12 email: '[email protected]') 13 @johnson = Contact.create(firstname: 'John', lastname: 'Johnson', 14 email: '[email protected]') 15 end 16 17 context "matching letters" do 18 # matching examples ... 19 end 20 21 context "non-matching letters" do 22 # non-matching examples ... 23 end 24 end 25 end 3. Model specs 27 RSpecs before hooks are vital to cleaning up nasty redundancy from your specs. As you might guess, the code contained within the before block is run before each example within the describe blockbut not outside of that block. Since weve indicated that the block should be run before each example, RSpec will create them for each example individually. In this example, my before block will only be called within the describe "filter last name by letter" blockin other words, my original validation specs will not have access to @smith, @jones, and @johnson. Speaking of my three test contacts, note that since they are no longer being created within each example, we have to assign them to instance variables, so theyre accessible outside of the before block, within our actual examples. If a spec requires some sort of post-example teardowndisconnecting from an external service, saywe can also use an after block to clean up after your examples. Since RSpec handles cleaning up the database, I rarely use after. before, though, is indispensable. Okay, lets see that full, organized spec: spec/models/contact_spec.rb 1 require 'spec_helper' 2 3 describe Contact do 4 it "is valid with a firstname, lastname and email" do 5 contact = Contact.new( 6 firstname: 'Aaron', 7 lastname: 'Sumner', 8 email: '[email protected]') 9 expect(contact).to be_valid 10 end 11 12 it "is invalid without a firstname" do 13 expect(Contact.new(firstname: nil)).to have(1).errors_on(:firstname) 14 end 15 16 it "is invalid without a lastname" do 17 expect(Contact.new(lastname: nil)).to have(1).errors_on(:lastname) 18 end 19 20 it "is invalid without an email address" do 21 expect(Contact.new(email: nil)).to have(1).errors_on(:lastname) 22 end 23 24 it "is invalid with a duplicate email address" do 25 Contact.create( 3. Model specs 28 26 firstname: 'Joe', lastname: 'Tester', 27 email: '[email protected]') 28 contact = Contact.new( 29 firstname: 'Jane', lastname: 'Tester', 30 email: '[email protected]') 31 expect(contact).to have(1).errors_on(:email) 32 end 33 34 it "returns a contact's full name as a string" do 35 contact = Contact.new(firstname: 'John', lastname: 'Doe', 36 email: '[email protected]') 37 expect(contact.name).to eq 'John Doe' 38 end 39 40 describe "filter last name by letter" do 41 before :each do 42 @smith = Contact.create(firstname: 'John', lastname: 'Smith', 43 email: '[email protected]') 44 @jones = Contact.create(firstname: 'Tim', lastname: 'Jones', 45 email: '[email protected]') 46 @johnson = Contact.create(firstname: 'John', lastname: 'Johnson', 47 email: '[email protected]') 48 end 49 50 context "matching letters" do 51 it "returns a sorted array of results that match" do 52 expect(Contact.by_letter("J")).to eq [@johnson, @jones] 53 end 54 end 55 56 context "non-matching letters" do 57 it "returns a sorted array of results that match" do 58 expect(Contact.by_letter("J")).to_not include @smith 59 end 60 end 61 end 62 end When we run the specs well see a nice outline (since we told RSpec to use the documentation format, in chapter 2) like this: 3. Model specs 29 Contact is invalid without an email address is invalid without a firstname is valid with a firstname, lastname and email is invalid without a lastname is invalid with a duplicate email address returns a contact's full name as a string filter last name by letter matching letters returns a sorted array of results that match non-matching letters returns a sorted array of results that match Phone does not allow duplicate phone numbers per contact allows two contacts to share a phone number Finished in 0.76147 seconds 10 examples, 0 failures Randomized with seed 62957 How DRY is too DRY? Weve spent a lot of time in this chapter organizing specs into easy-to-follow blocks. Like I said, before blocks are key to making this happenbut theyre also easy to abuse. When setting up test conditions for your example, I think its okay to bend the DRY principle in the interest of readability. If you find yourself scrolling up and down a large spec file in order to see what it is youre testing (or, later, loading too many external support files for your tests), consider duplicating your test data setup within smaller describe blocksor even within examples themselves. That said, well-named variables can go a long wayfor example, in the spec above we used @jones and @johnson as test contacts. These are much easier to follow than @user1 and @user2 would have been. Even better, when we get into testing users with specific roles in chapter 6, might be variables like @admin_user and @guest_user. Be expressive with your variable names! Summary This chapter focused on how I test models, but weve covered a lot of other important techniques youll want to use in other types of specs moving forward: 3. Model specs 30 Use active, explicit expectations: Use verbs to explain what an examples results should be. Only check for one result per example. Test for what you expect to happen and for what you expect to not happen: Think about both paths when writing examples, and test accordingly. Test for edge cases: If you have a validation that requires a password be between four and ten characters in length, dont just test an eight-character password and call it good. A good set of tests would test at four and eight, as well as at three and eleven. (Of course, you might also take the opportunity to ask yourself why youd allow such short passwords, or not allow longer ones. Testing is a good opportunity to reflect on an applications requirements and code.) Organize your specs for good readability: Use describe and context to sort similar examples into an outline format, and before and after blocks to remove duplication. However, in the case of tests readability trumps DRYif you find yourself having to scroll up and down your spec too much, its okay to repeat yourself a bit. With a solid collection of model specs incorporated into your app, youre well on your way to more trustworthy code. In the next chapter well apply and expand upon the techniques covered here to application controllers. Question When should I use describe versus context? From RSpecs perspective, you can use describe all the time, if youd like. Like many other aspects of RSpec, context exists to make your specs more readable. You could take advantage of this to match a condition, as Ive done in this chapter, or some other state in your application. Exercises So far weve assumed our specs arent returning false positivestheyve all gone from pending to passing without failing somewhere in the middle. Verify specs by doing the following: Comment out the code youre testing. For example, in our example that validates the presence of a contacts first name, we could comment out validates :firstname, presence: true, run the specs, and watch it "is invalid without a firstname" fail. Uncomment it to see the spec pass again. Edit the parameters passed to the factory within the expectation. This time, edit it "is invalid without a firstname" and give :firstname a non-nil value. The spec should fail; replace it with nil to see it pass again. http://lmws.net/describe-vs-context-in-rspec 4. Generating test data with factories So far weve been using plain old Ruby objects to create temporary data for our tests. And so far, our tests havent been so complex that much more than that has been necessary. As we test more complex scenarios, though, it sure would be nice to simplify that aspect of the process and focus more on the test instead of the data. Luckily, a handful of Ruby libraries exist to make test data generation easy. In this chapter well focus on Factory Girl, the preferred approach for many developers. Specifically: Well talk about the benefits and drawbacks of using factories as opposed to other methods. Then well create a basic factory and apply it to our existing specs. Following that well edit our factories to make them even more convenient to use. Next well create more realistic test data using the Faker gem. Well look at more advanced factories relying on Active Record associations. Finally, well talk about the risks of taking factory implementation too far in your applications. Check out the 04_factories branch of the sample source to follow along with the code samples. Using the command line, type git checkout -b 04_factories origin/04_factories. See chapter 1 for additional details. If you havent done so already, make sure youve got the factory_girl_rails and faker gems installed in your application, as outlined in chapter 2. Factories versus fixtures Out of the box, Rails provides a means of quickly generating sample data called fixtures. A fixture is essentially a YAML-formatted file which helps create sample data. For example, a fixture for our Contact model might look like 31 4. Generating test data with factories 32 contacts.yml 1 aaron: 2 firstname: "Aaron" 3 lastname: "Sumner" 4 email: "[email protected]" 5 6 john: 7 firstname: "John" 8 lastname: "Doe" 9 email: "[email protected]" Then, by referencing contacts(:aaron) in a test, Ive instantly got a fresh Contact with all attributes set. Pretty nice, right? Fixtures have their place, but also have their drawbacks. I wont spend a lot of time bad-mouthing fixturesfrankly, its already been done by plenty of people smarter than me in the Rails testing community. Long story short, there are two issues presented by fixtures Id like to avoid: First, fixture data can be brittle and easily broken (meaning you spend about as much time maintaining your test data as you do your tests and actual code); and second, Rails bypasses Active Record when it loads fixture data into your test database. What does that mean? It means that important things like your models validations are ignored. This is bad! Enter factories: Simple, flexible, building blocks for test data. If I had to point to a single component that helped me see the light toward testing more than anything else, it would be Factory Girl, an easy-to-use and easy-to-rely-on gem for creating test data without the brittleness of fixtures. Of course, the Ruby community is always up for a good debate on best practices, and Factory Girl also has its naysayers. In summer of 2012 an online debate over the merit of factories sprung up. A number of vocal opponents, including Rails creator David Heinemeier Hansson, pointed out that factories are a primary cause of slow test suites, and that factories can be particularly cumbersome with complex associations. While I see their point and acknowledge that the ease of using factories can come with a cost in terms of speed, I still believe that a slow test is better than no test, and that a factory-based approach simplifies things for people who are just learning how to test to begin with. You can always swap out factories for more efficient approaches later once youve got a suite built and are more comfortable with testing. In the meantime, lets put factories to work in our application. Since the factory_girl_rails gem installed Factory Girl for us as a dependency (see chapter 2), were ready to roll. https://github.com/thoughtbot/factory_girl https://groups.google.com/forum/?fromgroups#!topic/rubyonrails-core/_lcjRRgyhC0 4. Generating test data with factories 33 Adding factories to the application Back in the spec directory, add another subdirectory named factories; within it, add the file contacts.rb with the following content: spec/factories/contacts.rb 1 FactoryGirl.define do 2 factory :contact do 3 firstname "John" 4 lastname "Doe" 5 sequence(:email) { |n| "johndoe#{n}@example.com"} 6 end 7 end This chunk of code gives us a factory we can use throughout our specs. Essentially, whenever we create test data via FactoryGirl.create(:contact), that contacts name will be John Doe. His email address? Were using a handy feature provided by Factory Girl, called sequences. As you might have guessed from reading the code, a sequence will automatically increment n inside the block, yielding [email protected], [email protected], and so on as the factory is used to generate new contacts. Sequences are essential for any model that has a uniqueness validation. (Later in this chapter, well look at a nice alternative to generating things like email addresses and names, called Faker.) Filenames for factories arent as particular as those for specs. In fact, if you wanted to you could include all of your factories in a single file. However, the Factory Girl generator stores them in spec/factories as convention, with a filename thats the plural of the model it corresponds to (so, spec/factories/contacts.rb for the Contact model). I tend to just stick with that approach, too. Bottom line: As long as your factory definitions are syntactically correct and located in spec/factories/, you should be fine. With a solid factory in place, lets return to the contact_spec.rb file we set up in the previous chapter and add a quick example to it: 4. Generating test data with factories 34 spec/models/contact_spec.rb 1 require 'spec_helper' 2 3 describe Contact do 4 it "has a valid factory" do 5 expect(FactoryGirl.build(:contact)).to be_valid 6 end 7 8 ## more specs 9 end This instantiates a new contact with attributes as assigned by the factory. It then tests that new contacts validity. Lets revisit our existing specs, now using Factory Girl to streamline building our data. This time well override one or more attributes to generate data from factories, but with specific attributes: spec/models/contact_spec.rb it "is invalid without a firstname" do contact = FactoryGirl.build(:contact, firstname: nil) expect(contact).to have(1).errors_on(:firstname) end it "is invalid without a lastname" do contact = FactoryGirl.build(:contact, lastname: nil) expect(contact).to have(1).errors_on(:lastname) end it "is invalid without an email address" do contact = FactoryGirl.build(:contact, email: nil) expect(contact).to have(1).errors_on(:email) end it "returns a contact's full name as a string" do contact = FactoryGirl.build(:contact, firstname: "Jane", lastname: "Doe") expect(contact.name).to eq "Jane Doe" end 4. Generating test data with factories 35 These three are pretty straightforward. They all use Factory Girls build method to create a new, yet non-persisted, Contact. The first examples spec assigns contact to a Contact with no firstname assigned. The second follows suit, replacing the factorys default lastname with nil. Since our Contact model validates presence of both firstname and lastname, both of these examples expect to see errors. The third spec is a little different, but uses the same basic tools. This time, were creating a new Contact with specific values for firstname and lastname. Then, were making sure that the name method on the assigned contact returns the string we expect. The next spec throws in a minor wrinkle: spec/models/contact_spec.rb it "is invalid with a duplicate email address" do FactoryGirl.create(:contact, email: "[email protected]") contact = FactoryGirl.build(:contact, email: "[email protected]") expect(contact).to have(1).errors_on(:email) end In this example, were making sure the test objects email attribute is not duplicate data. In order to do this, we need another Contact persisted in the databaseso before running the expectation, we use FactoryGirl.create to first persist a contact with the same email address. Simplifying our syntax If theres one thing I know, its that programmers hate typing any more than they have to. And typing FactoryGirl.build(:contact) each time we need a new contact is already getting cumbersome. Luckily, Factory Girl beginning with version 3.0 makes the Rails programmers life a bit simpler with a little configuration. Add it anywhere inside the the RSpec.configure block located in spec_- helper.rb: spec/spec_helper.rb 1 RSpec.configure do |config| 2 # Include Factory Girl syntax to simplify calls to factories 3 config.include FactoryGirl::Syntax::Methods 4 5 # other configurations omitted ... 6 end 4. Generating test data with factories 36 Now our specs can use the shorter build(:contact) syntax. This one line of configuration also gives us create(:contact), which weve already used; and attributes_for(:contact) and build_stubbed(:contact), which well use in subsequent chapters. Heres a look at our updated, leaner model spec: spec/models/contact_spec.rb 1 require 'spec_helper' 2 3 describe Contact do 4 it "has a valid factory" do 5 expect(create(:contact)).to be_valid 6 end 7 8 it "is invalid without a firstname" do 9 expect(build(:contact, firstname: nil)).to \ 10 have(1).errors_on(:firstname) 11 end 12 13 it "is invalid without a lastname" do 14 expect(build(:contact, lastname: nil)).to \ 15 have(1).errors_on(:lastname) 16 end 17 18 # remaining examples omitted ... 19 end Much more readable, if you ask me, but entirely optional in your own code. Associations and inheritance in factories If we were to create a factory for our Phone model, given what we know so far, it might look something like this. 4. Generating test data with factories 37 spec/factories/phones.rb 1 FactoryGirl.define do 2 factory :phone do 3 association :contact 4 phone { '123-555-1234' } 5 phone_type 'home' 6 end 7 end Note the call to :association; that tells Factory Girl to create a new Contact for this phone to belong to if one wasnt passed into the build (or create) method. However, a contact can have three types of phoneshome, office, and mobile. So far, if we wanted to specify a non-home phone in a spec weve done it like this: spec/models/phone_spec.rb 1 it "allows two contacts to share a phone number" do 2 create(:home_phone, 3 phone: "785-555-1234") 4 expect(build(:home_phone, phone: "785-555-1234")).to be_valid 5 end Lets do some refactoring to clean this up. Factory Girl provides us the ability to create in- herited factories, overriding attributes as necessary. In other words, if we specifically want an office phone in a spec, we should be able to call it with build(:office_phone) (or the longer FactoryGirl.build(:office_phone), if you prefer). Heres how it looks: spec/factories/phones.rb 1 FactoryGirl.define do 2 factory :phone do 3 association :contact 4 phone { '123-555-1234' } 5 6 factory :home_phone do 7 phone_type 'home' 8 end 9 4. Generating test data with factories 38 10 factory :work_phone do 11 phone_type 'work' 12 end 13 14 factory :mobile_phone do 15 phone_type 'mobile' 16 end 17 end 18 end And the spec can be simplified to spec/models/phone_spec.rb 1 require 'spec_helper' 2 3 describe Phone do 4 it "does not allow duplicate phone numbers per contact" do 5 contact = create(:contact) 6 home_phone = create(:home_phone, 7 contact: contact, 8 phone: '785-555-1234') 9 mobile_phone = build(:mobile_phone, 10 contact: contact, 11 phone: '785-555-1234') 12 expect(mobile_phone).to have(1).errors_on(:phone) 13 end 14 15 it "allows two contacts to share a phone number" do 16 create(:home_phone, 17 phone: "785-555-1234") 18 expect(build(:home_phone, phone: "785-555-1234")).to be_valid 19 end 20 end This technique will come in handy in subsequent chapters when we need to create different user types (administrators versus non-administrators) for testing authentication and authorization mechanisms. 4. Generating test data with factories 39 Generating better fake data Earlier in this chapter, we used a sequence to make sure the contacts factory yielded unique email addresses. We can improve on this by providing more realistic test data to our app, using a fake data generator calledwhat else?Faker. Faker is a Ruby port of a time-honored Perl library for generating fake names, addresses, sentences, and moreexcellent for testing purposes. Lets incorporate some fake data into our factories: spec/factories/contacts.rb 1 require 'faker' 2 3 FactoryGirl.define do 4 factory :contact do 5 firstname { Faker::Name.first_name } 6 lastname { Faker::Name.last_name } 7 email { Faker::Internet.email } 8 end 9 end Now our specs will use a random email address each time the phone factory is used. (To see for yourself, check out log/test.log after running specs to see the email addresses that were inserted into the database in contact_spec.rb.) Two important things to notice: First, weve required the Faker library to load in the first line of my factory; and second, that we pass the Faker::Internet.email method inside a blockFactory Girl considers this a lazy attribute as opposed to the statically- added string the factory previously had. Lets wrap up this exercise by returning to that phone factory. Instead of giving every new phone a default number, lets give them all unique, random, realistic ones: spec/factories/phones.rb 1 require 'faker' 2 3 FactoryGirl.define do 4 factory :phone do 5 association :contact 6 phone { Faker::PhoneNumber.phone_number } 7 8 # child factories omitted ... 9 end 10 end 4. Generating test data with factories 40 Yes, this isnt strictly necessary. I could keep using sequences and my specs would still pass. But does give us a bit more realistic data with which to test. Faker can generate other types of random data such as addresses, phony business names and slogans, and lorem placeholder textrefer to the documentation for more. Check out Forgery as an alternative to Faker. Forgery performs a similar function but has a bit different syntax. Advanced associations The validation specs weve created so far have, for the most part, tested relatively simple aspects of our data. They havent required us to look at anything but the models in questionin other words, we havent validated that when we create a contact, three phone numbers also get created. How do we test that? And how do we make a factory to make sure our test contacts continue to represent realistic ones? The answer is to use Factory Girls callbacks to add additional code to a given factory. Callbacks are particularly useful in testing nested attributes, as in the way our user interface allows phone numbers to be entered upon creating or editing a contact. For example, this modification to our contact factory uses the after callback make sure that a new contact built with the factory will also have one each of the three phone types assigned to it: spec/factories/contacts.rb 1 require 'faker' 2 3 FactoryGirl.define do 4 factory :contact do 5 firstname { Faker::Name.first_name } 6 lastname { Faker::Name.last_name } 7 email { Faker::Internet.email } 8 9 after(:build) do |contact| 10 [:home_phone, :work_phone, :mobile_phone].each do |phone| 11 contact.phones << FactoryGirl.build(:phone, 12 phone_type: phone, contact: contact) http://rubydoc.info/gems/faker/1.0.1/frames 4. Generating test data with factories 41 13 end 14 end 15 end 16 end Note that after(:build) takes a block, and within that block, an array of our three phone types is used to also build a contacts phone numbers. We can make sure this is working with the following example: spec/models/contact_spec.rb 1 it "has three phone numbers" do 2 expect(create(:contact).phones.count).to eq 3 3 end This example passes, and existing examples pass as well, so changing the factory didnt break any of our existing work. We can even take this a step further, and add a validation inside the Contact model itself to make sure this happens: app/models/contact.rb validates :phones, length: { is: 3 } As an experiment, try changing the value in the validation to some other number, and run the test suite again. All of the examples that were expecting a valid contact will fail. As a second experiment, comment out the after block in the contact factory and run the test suiteagain, a whole lot of red. While this example may seem somewhat contrived, it does represent something youll sooner or later encounter in a complex application. In fact, this example is based on a scheduling system I built, requiring a user to add a minimum of two attendees to a meeting. It took me awhile to dig through the Factory Girl documentation, code, and Internet at large to get my factories working correctly with this requirement. after(:build) is just one callback now at our disposalas you might guess, we can also use before(:build), before(:create), and after(:create). They all work similarly. How to abuse factories Factories are great, except when theyre not. As mentioned at the beginning of this chapter, unchecked factory usage can cause a test suite to slow down in a hurryespecially when the 4. Generating test data with factories 42 complexities of associations are introduced. In fact, Id say that our last factorys creation of three additional objects every time it is called is pushing itbut at least at this point the convenience of generating that data with one method call instead of several outweighs any drawbacks. While generating associations with factories is an easy way to ramp up tests, its also an easy feature to abuse and often a culprit when test suites running times slow to a crawl. When that happens, its better to remove associations from factories and build up test data manually. You can also fall back to the Plain Old Ruby Objects approach we used in chapter 3, or even a hybrid approach combining them with factories. If youve looked at at other resources for testing in general or RSpec specifically, youve no doubt run across the terms mocks and stubs. If youve already got a bit of testing experience under your belt, you may wonder why Ive been using factories all this time and not mocks and stubs. The answer is because, from my experience, basic objects and factories are easier for getting developers started and comfortable with testingnot to mention, overuse of mocks and stubs can lead to a separate set of problems. Since at this stage our application is pretty small, any speed increase wed see with a fancier approach would be negligible. That said, mocks and stubs do have their roles in testing; well talk more about them in chapters 9 and 10. Summary Factory Girls been of good use to us in this chapter. Weve now got less syntax to clutter up our specs, a flexible way to create specific types of data, more realistic fake data, and a way to build more complex associations as needed. What you now know should get you through most testing tasks, but refer also to Factory Girls documentation for additional usage examplesFactory Girl could almost warrant its own short book. And while its not perfect, well be using Factory Girl throughout the remainder of the bookthe convenience it provides as we become more proficient in testing outweighs the issue of speed. In fact, it will play an important role in testing our next batch of code: The controllers that keep data moving between models and views. That will be the focus of the next chapter. Exercises Add factories to your application, if you havent done so aleady. Configure RSpec to use the shorter Factory Girl syntax in specs. How does doing so affect the readability of your examples? Take a look at your applications factories. How can you refactor them with inherited factories? https://github.com/thoughtbot/factory_girl/blob/master/GETTING_STARTED.md 4. Generating test data with factories 43 Do your models lend themselves to data types supported by Faker? Take another look at the Faker documentation if necessary, then apply Faker methods to your factories where applicable. Do your specs still pass? Do any models in your application use nested attributes? Would using the after(:build) callback result in more realistic test data? 5. Basic controller specs Poor controllers. As Rails developers we keep them skinny (which is a good thing) and often dont give them due attention in our tests (which is a bad thing; more on that in a moment). As you continue to improve your applications test coverage, though, controllers are the next logical chunk of code to tackle. Part of the challenge of testing controllers is they can be dependent on a number of other factorshow your models are configured to relate to one another, for example, or how you have your applications routing set up. Hang on, were going to address some of these challenges in this chapterbut once youve made it through youll have a clearer understanding of how to build controller specs in your own software. In this chapter, well begin covering a little more ground: First, well discuss why you should test controllers at all. Well follow that discussion with the very basics (or, controller specs are just unit specs). Next well begin organizing controller specs in an outline-like format. Well then use factories to set up data for specs. Then well test the seven CRUDmethods included in most controllers, along with a non-CRUD example. Next, well look at testing nested routes. Well wrap up with testing a controller method with non-HTML output, such as a file export. This chapter doesnt have sample code to followrather, its an intentionally simplified example. The real work on controller specs will start in the next chapterfor now, just follow along with some general observations about controller testing. Why test controllers? Following the lead of some prominent Ruby developers I stopped working on controller specs for awhile, in favor of covering this functionality in my feature specs (integration tests). At the time I liked this idea a lotusing tests that more closely mirrored how controller actions are accessed made good sensebut since then Ive come back to testing controllers more explicitly, for a few primary reasons: 44 5. Basic controller specs 45 Controllers are models too, as Piotr Solnica indicated in an excellent blog post. And in Rails applications, theyre pretty important modelsso its a good idea to put them on equal footing, spec-wise, as your Rails models. Controller specs can be written more quickly than their integration spec counterparts. For me, this becomes critical when I encounter a bug thats residing at the controller level, or I want to add additional specs to verify some refactoring. Writing a solid controller spec is a comparatively straightforward process, since I can generate very specific input to the method Im testing without the overhead of request specs. This also means that Controller specs usually run more quickly than request specs, making them very valuable during bug fixing and checking the bad paths your users can take (in addition to the good ones, of course). Controller testing basics Scaffolds, when done correctly, are a great way to learn coding techniques. The spec files generated for controllers, at least as of RSpec 2.8, are pretty nice and provide a good template to help you build your own specs. Look at the scaffold generator in rspec-rails source, or generate a scaffold in your properly-configured-for-RSpec-Rails application to begin getting a sense of these tests. (Another generator to look at is the one in Nifty Generators scaffolds). A controller spec is broken down by controller methodeach example is based off of a single action and, optionally, any params passed to it. Heres a simple example: it "redirects to the home page upon save" do post :create, message: FactoryGirl.attributes_for(:message) expect(response).to redirect_to root_url end You may notice similarities to earlier specs weve written: The description of the example is written in explicit, active language. The example only expects one thing: After the post request is processed, a redirect should be returned to the browser. A factory generates test data to pass to the controller method; note the use of Factory Girls attributes_for option, which generates a hash of values as opposed to a Ruby object. However, there are also a couple of new things to look at: http://solnic.eu/2012/02/02/yes-you-should-write-controller-tests.html https://github.com/rspec/rspec-rails/tree/master/lib/generators/rspec/scaffold https://github.com/ryanb/nifty-generators/tree/master/rails_generators/nifty_scaffold 5. Basic controller specs 46 The basic syntax of a controller specits HTTP method (post), controller method (:create), and, optionally, parameters being passed to the method. The aforementioned attributes_for call to Factory Girlnot rocket science, but worth mentioning again because I had a habit early on of forgetting to use it versus default factories. As a reminder, attributes_for() generates a hash of attributes, not an object. Organization Lets start with a top-down approach. As I mentioned earlier during our look at model specs, its helpful to think about a spec as an outline. Lets say our address book also has a scaffold to our application to handle the mostahemtrusting message board of all time. Anyone can post a message. Anyone can also view, edit, or delete anything written by someone else. This obviously wouldnt be a smart feature to implement on a real website, but we can use it to look at the anatomy of a controller spec. Heres what an outline of such a spec might contain: 1 require 'spec_helper' 2 3 describe MessagesController do 4 5 describe 'GET #index' do 6 it "populates an array of messages" 7 it "renders the :index view" 8 end 9 10 describe 'GET #show' do 11 it "assigns the requested message to @message" 12 it "renders the :show template" 13 end 14 15 describe 'GET #new' do 16 it "assigns a new Message to @message" 17 it "renders the :new template" 18 end 19 20 describe 'GET #edit' do 21 it "assigns the requested message to @message" 22 it "renders the :edit template" 23 end 24 25 describe "POST #create" do 5. Basic controller specs 47 26 context "with valid attributes" do 27 it "saves the new message in the database" 28 it "redirects to the home page" 29 end 30 31 context "with invalid attributes" do 32 it "does not save the new message in the database" 33 it "re-renders the :new template" 34 end 35 end 36 37 describe 'PUT #update' do 38 context "with valid attributes" do 39 it "updates the message in the database" 40 it "redirects to the message" 41 end 42 43 context "with invalid attributes" do 44 it "does not update the message" 45 it "re-renders the #edit template" 46 end 47 end 48 49 describe 'DELETE #destroy' do 50 it "deletes the message from the database" 51 it "redirects to the home page" 52 end 53 end As in our model specs, we can use RSpecs describe and context blocks to organize examples into a clean hierarchy, based on a controllers actions and the context were testingin this case, the happy path (a user passed valid attributes to the controller) and the unhappy path (a user passed invalid or incomplete attributes). Setting up test data Just as in model specs, controller specs need data. Here again well use factories to get startedonce youve got the hang of it you can swap these out with more efficient means of creating test data, but for our purposes (and this small app) factories will work great. Heres a factory to generate valid messages; lets add to it to include an invalid message child factory: 5. Basic controller specs 48 1 require 'faker' 2 3 FactoryGirl.define do 4 factory :message do 5 name { Faker::Name.name } 6 email { Faker::Internet.email } 7 message { Faker::Lorem.paragraph } 8 9 factory :invalid_message do 10 email nil 11 end 12 end 13 end Remember howwe used factory inheritance to create a :home_phone, :office_phone, and :mobile_- phone from a parent :phone factory? We can use that same technique to create an :invalid_message from the base :message factory. It replaces the specified attributes (in this case, email) with its own; everything else will defer to the original :message factory. Testing GET requests A standard Rails controller is going to have four GET-based methods: index, show, new, and edit. These methods are generally the easiest to test, as they should only be returning data to the browser. Lets start with index: 1 describe 'GET #index' do 2 it "populates an array of messages" do 3 message = create(:message) 4 get :index 5 expect(assigns(:messages)).to match_array [message] 6 end 7 8 it "renders the :index view" do 9 get :index 10 expect(response).to render_template :index 11 end 12 end Lets break this down. Were checking for two things here: First, that an array of messages is created and assigned to @messages. Using RSpecs assigns() method, we check that the collection (assigned to @messages) is what wed expect it to be with RSpecs match_array matcher. In this case, its 5. Basic controller specs 49 looking for a single-item array containing the message created within the example. The second example makes sure that the view template index.html.erb is rendered, via response. match_array looks for an arrays contents, but not their order. If order matters, use the eq matcher instead. The show examples are similar, with a slight twist: We need to pass along an id to look up in show: 1 describe 'GET #show' do 2 it "assigns the requested message to @message" do 3 message = create(:message) 4 get :show, id: message 5 expect(assigns(:message)).to eq message 6 end 7 8 it "renders the :show template" do 9 message = create(:message) 10 get :show, id: message 11 expect(response).to render_template :show 12 end 13 end This time, assigns() looks for @message to be assigned in the method, and to equal the message created prior to the request. The method should also render the show.html.erb template. new and edit are all that are left here; lets add them now: 1 describe 'GET #new' do 2 it "assigns a new Message to @message" do 3 get :new 4 expect(assigns(:message)).to be_a_new(Message) 5 end 6 7 it "renders the :new template" do 8 get :new 9 expect(response).to render_template :new 10 end 11 end 12 13 describe 'GET #edit' do 5. Basic controller specs 50 14 it "assigns the requested message to @message" do 15 message = create(:message) 16 get :edit, id: message 17 expect(assigns(:message)).to eq message 18 end 19 20 it "renders the :edit template" do 21 message = create(:message) 22 get :edit, id: message 23 expect(response).to render_template :edit 24 end 25 end Read through these examplesas you can see, once you know how to test one typical GET-based method, you can test most of them with a standard set of conventions. Testing POST requests Time to move on to our controllers create method, accessed via POST in our RESTful app. One key difference from the GET methods: Instead of the :id we passed to the GET methods, we need to pass the equivalent of params[:message]the contents of the form in which a user would enter a new message. As mentioned earlier, well use Factory Girls attributes_for() method. Heres the basic approach: it "does something upon post#create" do post :create, message: attributes_for(:message) end With that in mind, here are some specs for the method in questions. First, with valid attributes: 1 describe "POST #create" do 2 context "with valid attributes" do 3 it "saves the new message in the database" do 4 expect{ 5 post :create, message: attributes_for(:message) 6 }.to change(Message, :count).by(1) 7 end 8 9 it "redirects to the home page" do 10 post :create, message: attributes_for(:message) 5. Basic controller specs 51 11 expect(response).to redirect_to root_url 12 end 13 end And close out the block with invalid attributes: 1 context "with invalid attributes" do 2 it "does not save the new message in the database" do 3 expect{ 4 post :create, 5 message: attributes_for(:invalid_message) 6 }.to_not change(Message, :count) 7 end 8 9 it "re-renders the :new template" do 10 post :create, 11 message: attributes_for(:invalid_message) 12 expect(response).to render_template :new 13 end 14 end 15 end There are a couple of things to take note of in this code. First, notice the use of context blocks, as first introduced in chapter 3. Remember, although describe and context may be used interchangeably, its considered best practice to use context when describing different statesin this case, one state with valid attributes, and one with invalid attributes. The examples using invalid attributes use the :invalid_message factory we set up way back at the beginning of this chapter. Second, notice the slight difference in how were using expect in the first example. This time, were passing the full HTTP request to expect{}. This is slightly more complex than how weve been using expect so far. The HTTP request is passed in as a Proc, and the results are evaluated before and after, making it simple to determine whether the anticipated change happenedor in the case of this example, did not happen. As usual, though, RSpecs readability shines hereexpect this code to (or to not) do something. This one little example succinctly tests that an object is created and stored. Become familiar with this technique, as itll be very useful in testing a variety of methods in controllers, models, and eventually at the integration level. If Proc objects seem magical to you, there are some excellent blog posts on the topic from Alan Skorkin and Robert Sosinksi to make them less so. 5. Basic controller specs 52 Testing PUT requests On to our controllers update method, where we need to check on a couple of thingsfirst, that the attributes passed into the method get assigned to the model we want to update; and second, that the redirect works as we want. 1 describe 'PUT #update' do 2 before :each do 3 @message = create(:message, name: "Aaron Sumner", 4 email: "[email protected]") 5 end 6 7 it "locates the requested @message" do 8 put :update, id: @message, message: attributes_for(:message) 9 expect(assigns(:message)).to eq(@message) 10 end 11 12 context "valid attributes" do 13 it "changes @message's attributes" do 14 put :update, id: @message, 15 message: attributes_for(:message, 16 firstname: "A. Sumner") 17 @message.reload 18 expect(@message.name).to eq("A. Sumner") 19 end 20 21 it "redirects to the updated message" do 22 put :update, id: @message, message: attributes_for(:message) 23 expect(response).to redirect_to @message 24 end 25 end Then, as we did in the previous POST examples, we need to test that those things dont happen if invalid attributes are passed through the params: 5. Basic controller specs 53 1 context "invalid attributes" do 2 it "does not change @message's attributes" do 3 put :update, id: @message, 4 message: attributes_for(:message, 5 name: "None of your business", 6 email: nil) 7 @message.reload 8 expect(@message.name).to_not eq("None of your business") 9 end 10 11 it "re-renders the edit method" do 12 put :update, id: @message, message: attributes_for(:invalid_message) 13 expect(response).to render_template :edit 14 end 15 end 16 end The examples I want to point out here are the two that verify whether or not an objects attributes are actually changed by the update methodwe cant use the expect{} proc here. Note that we have to call reload on @message to check that our updates are actually persisted. Otherwise, these examples follow a similar pattern to the one we used in the POST-related specs. Testing DESTROY requests After all that, testing the destroy method is relatively straightforward: 1 describe 'DELETE destroy' do 2 before :each do 3 @message = create(:message) 4 end 5 6 it "deletes the message" do 7 expect{ 8 delete :destroy, id: @message 9 }.to change(Message,:count).by(-1) 10 end 11 12 it "redirects to messages#index" do 13 delete :destroy, id: @message 14 expect(response).to redirect_to messages_url 15 end 16 end 5. Basic controller specs 54 By now you should be able to correctly guess what everythings doing. The first expectation checks to see if the destroy method in the controller actually deleted the object (using the now-familiar expect{} proc); the second expectation confirms that the user is redirected back to the index upon success. Testing non-CRUD methods Testing a controllers other methods isnt much different from testing the standard, out-of-the-box RESTful resources Rails gives us. Lets say weve got a method to allow visitors to flag messages as inappropriate with the click of a button. We could test this at the controller level with something like 1 describe "PUT flag_as_inappropriate" do 2 before :each do 3 @message = create(:message) 4 end 5 6 it "marks the message as inappropriate" do 7 put :flag_as_inappropriate, id: @message 8 expect(@message.reload.is_inappropriate?).to be_true 9 end 10 11 it "redirects to messages#index" do 12 put :flag_as_inappropriate, id: @message 13 expect(response).to redirect_to messages_url 14 end 15 end See what were doing? Were using the HTTP put methodsince were editing an existing mes- sagealong with :flag_as_inappropriate to indicate the controller method to access. Everything else works similarly to testing the update method, with the exception that were not passing a hash of user-entered attributesin this example, the is_inappropriate? boolean is set server-side. expect(@message.reload.is_inappropriate?).to be_true is a good candidate for a custom matcher. Well visit this concept in chapter 7. If your method uses one of the other HTTP request methods, just follow along with its respective CRUD-based approach to test it. 5. Basic controller specs 55 Testing nested routes If your application uses nested routesthat is, a route that looks something like /contacts/34/appointments/22, youll need to provide your examples with a little more information. See Rails Routing from the Outside In for an excellent overview of nested routes. Going back to the sample route from the previous paragraph, it looks like weve got appointments nested within contacts. If you were to look at the routes for the app (with rake routes, via the command line) youd see that the path translates to /contacts/:contact_id/appointments/:idso we need to pass in an appointments :id and a :contact_id for its parent contact. Heres how that would look in a spec: 1 describe 'GET #show' do 2 it "renders the :show template for the appointment" do 3 contact = create(:contact) 4 appointment = create(:appointment, contact: contact) 5 get :show, id: appointment, contact_id: contact.id 6 expect(response).to render_template :show 7 end 8 end The key thing to remember is you need to pass the parent route to the server in the form of :parent_- idin this case, :contact_id. The controller will handle things from there, as directed by your routes.rb file. This same basic technique applies to any method in a nested controller. Testing non-HTML controller output So far weve just been testing a controller methods HTML output. Of course, Rails lets us send multiple data types from a single controller method in addition toor instead ofHTML. Continuing with our message board example, lets say we need to export messages to a CSV file. If youre already returning content in a non-HTML format, you probably know how to override the HTML default in a given methods route: link_to 'Export', messages_path(format: :csv) This would assume a controller method along these lines: 5. Basic controller specs 56 1 def index 2 @messages = Message.all 3 4 respond_to do |format| 5 format.html # index.html.erb 6 format.csv do 7 send_data Message.to_csv(@messages), 8 type: 'text/csv; charset=iso-8859-1; header=present', 9 :disposition => 'attachment; filename=messages.csv' 10 end 11 end 12 end A simple means of testing this, then, is to verify the data type: 1 describe 'CSV output' do 2 it "returns a CSV file" do 3 get :index, format: :csv 4 expect(response.headers['Content-Type']).to have_content 'text/csv' 5 end 6 7 it 'returns content' do 8 create(:message, 9 name: 'Aaron', 10 email: '[email protected]', 11 message: 'Testing!') 12 get :index, format: :csv 13 expect(response.body).to have_content 'Aaron,[email protected],Testing!' 14 end 15 end The have_content matcher shown here comes from Capybara, which well cover with more depth in chapter 8. This will verify that the controller is returning the CSV data with the proper content type. However, given the structure were using to actually generate CSV contentthat is, with a class method on Contact, testing that functionality at the model level (as opposed to the controller layer) is perhaps the ideal way to go: 5. Basic controller specs 57 1 it "returns comma separated values" do 2 create(:message, name: 'Aaron', 3 email: '[email protected]', 4 message: 'Testing!') 5 expect(Message.to_csv).to match /Aaron Sumner,[email protected],Testing!/ 6 end Note the use of RSpecs match matcher here, used whenever a regular expression is being compared to the actual results. To see the general approach Ive used to generate CSV-formatted data, see Railscasts episode 362, Exporting to CSV and Excel. If youre using Rails to serve up an API, its also possible to test JSON or XML output with relative ease at the controller level: 1 it "returns JSON-formatted content" do 2 message = create(:message) 3 get :index, format: :json 4 expect(response.body).to have_content message.to_json 5 end The key when using this approach is to keep your code nice and modular, so you can directly test the class in question (in this case, a controller class). From there you might want to explore Rack::Test. If youre using the API to render a JavaScript-based interface within your own application, then using Capybara and feature specs could be a useful addition to your test suite (well cover feature specs and Capybara in chapter 8). A complete example is beyond the scope of this book, but a good resource for testing JSON APIs in Rails is Rails 3 in Action by Ryan Bigg and Yehuda Katz. You may also want to explore client-side test libraries like Jasmine. Noel Rappins Master Space and Time with JavaScript series is a good resource to learn about testing at this level. http://rubygems.org/gems/rack-test http://manning.com/katz/ https://github.com/pivotal/jasmine https://noelrappin.dpdcart.com/ 5. Basic controller specs 58 Summary In a nutshell, thats how you test your applications controllers. The key is to break down what you need to test, and then incrementally build those tests until youve got your functionality covered. Unfortunately, controller specs arent always this straightforward. Often youll need to contend with user logins, additional, non-scaffolded code, or models with particular validation requirements. Thats what well cover next. Exercises How would you test the :invalid_message factory? If youre noticing youve got to do quite a bit of setup just to test a single controller method, it could be a sign that your controller needs refactoring. Perhaps theres code contained within the controller method that would be better-suited in a model or helper method. Take opportunities to clean up your code as they present themselvesmove the offending code to a model, spec it there as needed, and simplify your controller specwhich should still pass. 6. Advanced controller specs With the basics of controller testing out of the way, its time to return to some real code and look at how RSpec helps make sure our applications controllers are doing what we expect them to do. This time, though, well build onto the vanilla CRUD specs by accounting for our applications authentication and authorization layers. In a bit more detail: Well start by setting up a more complicated spec. Next well cover testing authentication through the controller. Well follow that by testing authorization, or roles; also through the controller. Well also look at a technique for making sure controller specs are properly handling any additional setup requirements your application might have. Check out the 06_advanced_controllers branch of the sample source to followalong with the code samples. Using the command line, type git checkout -b 03_models origin/06_advanced_- controllers. See chapter 1 for additional details. Setting up the spec In the previous chapter, we began looking at controller specs by outlining our expectations of the controller. Returning to our address book app as an example, here are some things we might need to test. Note that its quite a bit more complex than the simple outline we worked from in the last chapterthis time weve also got to contend with whether a user is logged in or not, and the logged- in users role (as a reminder, you must be a user to add or edit, and you must be an administrative user to add other users). Well use Ryan Bates CanCan library to handle this authorization layer in the app itself. Yes, I know theres a lot of duplication herework with me for now; well clean it up once some groundwork has been established. https://github.com/ryanb/cancan 59 6. Advanced controller specs 60 spec/controllers/contacts_controller_spec.rb 1 require 'spec_helper' 2 3 describe ContactsController do 4 5 describe "guest access" do 6 describe 'GET #index' do 7 it "populates an array of contacts" 8 it "renders the :index view" 9 end 10 11 describe 'GET #show' do 12 it "assigns the requested contact to @contact" 13 it "renders the :show template" 14 end 15 16 describe 'GET #new' do 17 it "requires login" 18 end 19 20 describe "POST #create" do 21 it "requires login" 22 end 23 24 describe 'PUT #update' do 25 it "requires login" 26 end 27 28 describe 'DELETE #destroy' do 29 it "requires login" 30 end 31 end 32 33 describe "admin access" do 34 describe 'GET #index' do 35 it "populates an array of contacts" 36 it "renders the :index view" 37 end 38 39 describe 'GET #show' do 40 it "assigns the requested contact to @contact" 6. Advanced controller specs 61 41 it "renders the :show template" 42 end 43 44 describe 'GET #new' do 45 it "assigns a new Contact to @contact" 46 it "renders the :new template" 47 end 48 49 describe "POST #create" do 50 context "with valid attributes" do 51 it "saves the new contact in the database" 52 it "redirects to the home page" 53 end 54 55 context "with invalid attributes" do 56 it "does not save the new contact in the database" 57 it "re-renders the :new template" 58 end 59 end 60 61 describe 'PUT #update' do 62 context "with valid attributes" do 63 it "updates the contact in the database" 64 it "redirects to the contact" 65 end 66 67 context "with invalid attributes" do 68 it "does not update the contact" 69 it "re-renders the #edit template" 70 end 71 end 72 end 73 74 describe "user access" do 75 describe 'GET #index' do 76 it "populates an array of contacts" 77 it "renders the :index view" 78 end 79 80 describe 'GET #show' do 81 it "assigns the requested contact to @contact" 82 it "renders the :show template" 6. Advanced controller specs 62 83 end 84 85 describe 'GET #new' do 86 it "assigns a new Contact to @contact" 87 it "renders the :new template" 88 end 89 90 describe "POST #create" do 91 context "with valid attributes" do 92 it "saves the new contact in the database" 93 it "redirects to the home page" 94 end 95 96 context "with invalid attributes" do 97 it "does not save the new contact in the database" 98 it "re-renders the :new template" 99 end 100 end 101 102 describe 'PUT #update' do 103 context "with valid attributes" do 104 it "updates the contact in the database" 105 it "redirects to the contact" 106 end 107 108 context "with invalid attributes" do 109 it "does not update the contact" 110 it "re-renders the #edit template" 111 end 112 end 113 114 describe 'DELETE #destroy' do 115 it "deletes the contact from the database" 116 it "redirects to the home page" 117 end 118 end 119 end Looks like weve got a lot of ground to cover! However, this hopefully illustrates why controller specs shouldnt be overlookedtheyre an excellent way to rapidly test a number of specific scenarios. With some organization in place, lets go over some things you might want to test in your application and how those tests would actually work. 6. Advanced controller specs 63 Setting up data In the last chapter we used Factory Girl inheritance to create invalid datalets apply that technique in our application by creating an :invalid_contact factory, inheriting from the :contact factory: spec/factories/contacts.rb 1 require 'faker' 2 3 FactoryGirl.define do 4 factory :contact do 5 firstname { Faker::Name.first_name } 6 lastname { Faker::Name.last_name } 7 email { Faker::Internet.email } 8 9 after(:build) do |contact| 10 [:home_phone, :work_phone, :mobile_phone].each do |phone| 11 contact.phones << FactoryGirl.build(:phone, 12 phone_type: phone, contact: contact) 13 end 14 end 15 16 factory :invalid_contact do 17 firstname nil 18 end 19 end 20 end Testing the guest role Were going to take a different approach to walking through the controller spec this time. Well go through each possible roleguest, user, and administrator. Lets begin with the guest, or a user whos not logged in. These examples are easy to write: 6. Advanced controller specs 64 spec/controllers/contacts_controller_spec.rb 1 describe "guest access" do 2 describe 'GET #index' do 3 it "populates an array of contacts" do 4 contact = create(:contact) 5 get :index 6 expect(assigns(:contacts)).to match_array [contact] 7 end 8 9 it "renders the :index view" do 10 get :index 11 expect(response).to render_template :index 12 end 13 end 14 15 describe 'GET #show' do 16 it "assigns the requested contact to @contact" do 17 contact = create(:contact) 18 get :show, id: contact 19 expect(assigns(:contact)).to eq contact 20 end 21 22 it "renders the :show template" do 23 contact = create(:contact) 24 get :show, id: contact 25 expect(response).to render_template :show 26 end 27 end 28 29 describe "GET #new" do 30 it "requires login" do 31 get :new 32 expect(response).to redirect_to login_url 33 end 34 end 35 36 describe "GET #edit" do 37 it "requires login" do 38 get :edit, id: create(:contact) 39 expect(response).to redirect_to login_url 40 end 6. Advanced controller specs 65 41 end 42 43 describe "POST #create" do 44 it "requires login" do 45 post :create, id: create(:contact), 46 contact: attributes_for(:contact) 47 expect(response).to redirect_to login_url 48 end 49 end 50 51 describe 'PUT #update' do 52 it "requires login" do 53 put :update, id: create(:contact), contact: attributes_for(:contact) 54 expect(response).to redirect_to login_url 55 end 56 end 57 58 describe 'DELETE #destroy' do 59 it "requires login" do 60 delete :destroy, id: create(:contact) 61 expect(response).to redirect_to login_url 62 end 63 end 64 end Nothing new until we hit newthe first method the controllers before_filter requires login to access. This time, we need to make sure guests cant do the things in the controller methodinstead, they should be redirected to the login_url, at which point they will be asked to sign in. As you can see, we can use this technique on any method that requires login. As an experiment, you can comment out the before_filter in the controller and run specs again to see what happens. You can also change expect(response).to redirect_to login_url to expect(response).to_not redirect_to login_url, or change login_url to a different path. Its a good idea to intentionally break things like this, to help reduce the chance a false positive in a test gets past you. 6. Advanced controller specs 66 Testing the admin and user roles Users and administrators (users with the :admin boolean enabled) have identical permissions for contacts. Any user who has signed in with a valid account can create, edit, and delete any contact. First, we need a factory for users: spec/factories/users.rb 1 require 'faker' 2 3 FactoryGirl.define do 4 factory :user do 5 email { Faker::Internet.email } 6 password 'secret' 7 8 factory :admin do 9 admin true 10 end 11 end 12 end Now, back to the controller spec, lets use the factory to test administrator access: spec/controllers/contacts_controller_spec.rb 1 describe "administrator access" do 2 before :each do 3 @contact = create(:contact, firstname: 'Lawrence', lastname: 'Smith') 4 user = create(:admin) 5 session[:user_id] = user.id 6 end 7 8 describe 'GET #index' do 9 it "populates an array of contacts" do 10 get :index 11 expect(assigns(:contacts)).to match_array [@contact] 12 end 13 14 it "renders the :index view" do 15 get :index 6. Advanced controller specs 67 16 expect(response).to render_template :index 17 end 18 end 19 20 describe 'GET #show' do 21 it "assigns the requested contact to @contact" do 22 get :show, id: @contact 23 expect(assigns(:contact)).to eq @contact 24 end 25 26 it "renders the :show template" do 27 get :show, id: @contact 28 expect(response).to render_template :show 29 end 30 end 31 32 # additional examples So far, so goodthis access is the same as wed already tested for guests. Again, we could test our tests by changing expectations, to verify our specs are testing what they should. Notice that Im using a before :each block to define a @contact Ill use throughout these specs, then I log in as an administrator (using an :admin factory, inherited from :user). Beginning with new weve got new specs to work withbecause this time around, were simulating an actual user being logged in and thus able to access the otherwise blocked methods: spec/controllers/contacts_controller_spec.rb 1 describe 'GET #new' do 2 it "assigns a new Contact to @contact" do 3 get :new 4 expect(assigns(:contact)).to be_a_new(Contact) 5 end 6 7 it "assigns a home, office, and mobile phone to the new contact" do 8 get :new 9 phones = assigns(:contact).phones.map do |p| 10 p.phone_type 11 end 12 expect(phones).to match_array %w(home office mobile) 13 end 14 6. Advanced controller specs 68 15 it "renders the :new template" do 16 get :new 17 expect(response).to render_template :new 18 end 19 end 20 21 describe 'GET #edit' do 22 it "assigns the requested contact to @contact" do 23 get :edit, id: @contact 24 expect(assigns(:contact)).to eq @contact 25 end 26 27 it "renders the :edit template" do 28 get :edit, id: @contact 29 expect(response).to render_template :edit 30 end 31 end The contact controllers GET methods dont differ a whole lot from the example we looked at last chapter, with one exception. Since A new contacts phones are built in the new method, we can test to make sure those new phones are actually being built and assigned to the new @contact. Otherwise, the specs for new and edit are pretty standard. Here are our examples for the contacts controllers create method. spec/controllers/contacts_controller_spec.rb 1 describe "POST #create" do 2 before :each do 3 @phones = [ 4 attributes_for(:phone, phone_type: "home"), 5 attributes_for(:phone, phone_type: "office"), 6 attributes_for(:phone, phone_type: "mobile") 7 ] 8 end 9 10 context "with valid attributes" do 11 it "creates a new contact" do 12 expect{ 13 post :create, contact: attributes_for(:contact, 14 phones_attributes: @phones) 15 }.to change(Contact,:count).by(1) 6. Advanced controller specs 69 16 end 17 18 it "redirects to the new contact" do 19 post :create, contact: attributes_for(:contact, 20 phones_attributes: @phones) 21 expect(response).to redirect_to Contact.last 22 end 23 end 24 25 context "with invalid attributes" do 26 it "does not save the new contact" do 27 expect{ 28 post :create, contact: attributes_for(:invalid_contact) 29 }.to_not change(Contact,:count) 30 end 31 32 it "re-renders the new method" do 33 post :create, contact: attributes_for(:invalid_contact) 34 expect(response).to render_template :new 35 end 36 end 37 end First off, whats up with that before :each block creating an array of phone hashes? Remember, our :contact controller uses after_build to populate a contacts phone numbers. Since attributes_for never makes it to buildits just setting up the attributes, not instantiating a new contactwe need to help it along. This is likely an edge casein all of the Rails applications Ive written over the years, Ive only come across a couple of instances in which setting things up like this was necessary. However, its good to know in case you run across a similar situation. Aside from that, the create specs use the expect{} proc we looked at in the last chapter. Next up are the controllers update specs. spec/controllers/contacts_controller_spec.rb 1 describe 'PUT #update' do 2 context "valid attributes" do 3 it "located the requested @contact" do 4 put :update, id: @contact, contact: attributes_for(:contact) 5 expect(assigns(:contact)).to eq(@contact) 6 end 7 6. Advanced controller specs 70 8 it "changes @contact's attributes" do 9 put :update, id: @contact, 10 contact: attributes_for(:contact, 11 firstname: "Larry", lastname: "Smith") 12 @contact.reload 13 expect(@contact.firstname).to eq("Larry") 14 expect(@contact.lastname).to eq("Smith") 15 end 16 17 it "redirects to the updated contact" do 18 put :update, id: @contact, contact: attributes_for(:contact) 19 expect(response).to redirect_to @contact 20 end 21 end 22 23 context "invalid attributes" do 24 it "locates the requested @contact" do 25 put :update, id: @contact, contact: attributes_for(:invalid_contact) 26 expect(assigns(:contact)).to eq(@contact) 27 end 28 29 it "does not change @contact's attributes" do 30 put :update, id: @contact, 31 contact: attributes_for(:contact, 32 firstname: "Larry", lastname: nil) 33 @contact.reload 34 expect(@contact.firstname).to_not eq("Larry") 35 expect(@contact.lastname).to eq("Smith") 36 end 37 38 it "re-renders the edit method" do 39 put :update, id: @contact, contact: attributes_for(:invalid_contact) 40 expect(response).to render_template :edit 41 end 42 end 43 end Like the example from the last chapter, were testing a few different things at this level. Dont forget to call reload on the object youre testing, to update whats in memory with whatever was added to the database as the controller method did its thing. Im reasonably happy with the specs on update, though for complete coverage well want to make sure to include the happy path functionality in our request specs in a couple of chapters. 6. Advanced controller specs 71 Lets close out with a couple of expectations for the destroy methodnothing too fancy here. spec/controllers/contacts_controller_spec.rb 1 describe 'DELETE destroy' do 2 it "deletes the contact" do 3 expect{ 4 delete :destroy, id: @contact 5 }.to change(Contact,:count).by(-1) 6 end 7 8 it "redirects to contacts#index" do 9 delete :destroy, id: @contact 10 expect(response).to redirect_to contacts_url 11 end 12 end For the sake of brevity, Im not going to include a full non-administrators permission specs. Review the sample code for this chapter for a complete implementationaside from before :each block the expectations are exactly the same. spec/controllers/contacts_controller_spec.rb 1 describe "user access" do 2 before :each do 3 @contact = create(:contact, firstname: 'Lawrence', lastname: 'Smith') 4 user = create(:user) 5 session[:user_id] = user.id 6 end 7 8 # specs are the same as administrator Yes, this is a lot of redundant code. Dont worry, RSpec has a feature to deal with thiswell get to it in the next chapter. Testing a given roles authorization Well need to consider a different controller to see how to spec a given users authorizationthat is, what he or she is allowed to do upon successful login. In the sample application, only administrators 6. Advanced controller specs 72 may add new users. Regular usersthose without the :admin boolean switched onshould be denied access. The approach is basically the same one weve followed so far: Set up a user to simulate in a before :each block, simulate the login by assigning the user_id to a session variable in the before :each block, then write the specs. This time, though, instead of redirecting to the login form, users should be redirected back to the applications root URL (this is the behavior defined in app/controllers/application_controller.rb, when a CanCan::AccessDenied exception gets raised). Here are some specs for this scenario: spec/controllers/users_controller_spec.rb 1 describe 'user access' do 2 before :each do 3 session[:user_id] = create(:user).id 4 end 5 6 it "GET#index denies access" do 7 get :index 8 expect(response).to redirect_to root_url 9 end 10 11 it "GET#new denies access" do 12 get :new 13 expect(response).to redirect_to root_url 14 end 15 16 it "POST#create denies access" do 17 post :create, user: attributes_for(:user) 18 expect(response).to redirect_to root_url 19 end 20 end Summary Weve covered an awful lot in this chapterbut the fact is, you can test an awful lot of your applications functionality by applying good test coverage at the controller level. Now that Ive shared some of the many things you can test in your controllers, let me be honest with youit wasnt until recently that I began testing at this level of my apps with such thoroughness. In fact, for a long time my controller specs just tested a few basics. But as you can see from RSpecs generated examples, there are several things you canand shouldtest at the controller level. 6. Advanced controller specs 73 And with thoroughly tested controllers, youre well on your way to thorough test coverage in your application as a whole. By now you should be getting a handle on good practices and techniques for the practical use of RSpec, Factory Girl, and other helpers to make your tests and code more reliable. We can still do better, thoughlets go through this spec one more time and clean it up through helper methods and shared examples. Exercise For a given controller in your application, sketch a table of which methods should be accessible to which users. For example, say I have a blogging application for premium contentusers must become members to access content, but can get a feel for what theyre missing by seeing a list of titles. Actual users have different levels of access based on their respective roles. The hypothetical apps Posts controller might have the following permissions: Role Index Show Create Update Destroy Admin Full Full Full Full Full Editor Full Full Full Full Full Author Full Full Full Full None Member Full Full None None None Guest Full None None None None Use this table to help figure out the various scenarios that need to be tested. In this example I merged new and create into one column (since it doesnt make much sense to render the new form if it cant be used to create anything), as well as edit and update, while splitting index and show. How would these compare to your applications authentication and authorization requirements? What would you need to change? 7. Controller spec cleanup If youve been applying what youve learned so far to your own code, youre well on your way to a solid test suite. However, in the last chapter we introduced a lot of repetitionand potentially brittle tests. What would happen, say, if instead of redirecting unauthorized requests to root_path, we created a specific denied_path route? Weve have a lot of individual specs to clean up. Just as you would your application code, you should take opportunities to clean up your specs. In this chapter well look at three ways to reduce redundancy and brittleness, without sacrificing readability: To start, well share examples across multiple describe and context blocks. Next well reduce more repetition with helper macros. Well finish up by creating custom RSpec matchers. Check out the 07_controller_cleanup branch of the sample source to follow along with the code samples. Using the command line, type git checkout -b 07_controller_cleanup origin/07_- controller_cleanup. See chapter 1 for additional details. Shared examples Way back in chapter 1, when discussing my general approach to testing, I said a readable spec is ultimately more important than a 100 percent DRY spec. I stand by thatbut looking at contacts_- controller_spec.rb, somethings got to give. As it stands right now, weve got many examples included twice (once for administrators; once for regular users), and some examples are included thriceguests, admins, and regular users may all access the :index and :show methods. Thats a lot of code, and it jeopardizes readability and long-term maintainability. RSpec gives us a nice way to clean up this replication with shared examples. Setting up a shared example is pretty simplefirst, create a block for the examples as follows: 74 7. Controller spec cleanup 75 spec/controllers/contacts_controller_spec.rb 1 shared_examples("public access to contacts") do 2 describe 'GET #index' do 3 it "populates an array of contacts" do 4 get :index 5 expect(assigns(:contacts)).to match_array [@contact] 6 end 7 8 it "renders the :index view" do 9 get :index 10 expect(response).to render_template :index 11 end 12 end 13 14 describe 'GET #show' do 15 it "assigns the requested contact to @contact" do 16 get :show, id: @contact 17 expect(assigns(:contact)).to eq @contact 18 end 19 20 it "renders the :show template" do 21 get :show, id: @contact 22 expect(response).to render_template :show 23 end 24 end 25 end Then include them in any describe or context block in which youd like to use the examples, like this (actual code removed for clarity): spec/controllers/contacts_controller_spec.rb 1 describe "guest access" do 2 it_behaves_like "public access to contacts" 3 4 # rest of specs for guest access ... 5 end As a result of this exercise, our contacts_controller_spec.rb is much cleaner, as you can see in this outline: 7. Controller spec cleanup 76 spec/controllers/contacts_controller_spec.rb 1 require 'spec_helper' 2 3 describe ContactsController do 4 shared_examples("public access to contacts") do 5 describe 'GET #index' do 6 it "populates an array of contacts" 7 it "renders the :index view" 8 end 9 10 describe 'GET #show' do 11 it "assigns the requested contact to @contact" 12 it "renders the :show template" 13 end 14 end 15 16 shared_examples("full access to contacts") do 17 describe 'GET #new' do 18 it "assigns a new Contact to @contact" 19 it "assigns a home, office, and mobile phone to the new contact" 20 it "renders the :new template" 21 end 22 23 describe 'GET #edit' do 24 it "assigns the requested contact to @contact" 25 it "renders the :edit template" 26 end 27 28 describe "POST #create" do 29 context "with valid attributes" do 30 it "creates a new contact" 31 it "redirects to the new contact" 32 end 33 34 context "with invalid attributes" do 35 it "does not save the new contact" 36 it "re-renders the new method" 37 end 38 end 39 40 describe 'PUT #update' do 7. Controller spec cleanup 77 41 context "valid attributes" do 42 it "located the requested @contact" 43 it "changes @contact's attributes" 44 it "redirects to the updated contact" 45 end 46 47 context "invalid attributes" do 48 it "locates the requested @contact" 49 it "does not change @contact's attributes" 50 it "re-renders the edit method" 51 end 52 end 53 54 describe 'DELETE destroy' do 55 it "deletes the contact" 56 it "redirects to contacts#index" 57 end 58 end 59 60 describe "admin access to contacts" do 61 before :each do 62 set_user_session(create(:admin)) 63 end 64 65 it_behaves_like "public access to contacts" 66 it_behaves_like "full access to contacts" 67 end 68 69 describe "user access to contacts" do 70 before :each do 71 set_user_session(create(:user)) 72 end 73 74 it_behaves_like "public access to contacts" 75 it_behaves_like "full access to contacts" 76 end 77 78 describe "guest access to contacts" do 79 it_behaves_like "public access to contacts" 80 81 describe 'GET #new' do 82 it "requires login" 7. Controller spec cleanup 78 83 end 84 85 describe "POST #create" do 86 it "requires login" 87 end 88 89 describe 'PUT #update' do 90 it "requires login" 91 end 92 93 describe 'DELETE #destroy' do 94 it "requires login" 95 end 96 end 97 end And when we run bundle exec rspec controllers/contacts_controller_spec.rb, the docu- mentation output is just as readable: 1 ContactsController 2 admin access to contacts 3 behaves like public access to contacts 4 GET #index 5 populates an array of contacts 6 renders the :index view 7 GET #show 8 assigns the requested contact to @contact 9 renders the :show template 10 behaves like full access to contacts 11 GET #new 12 assigns a new Contact to @contact 13 assigns a home, office, and mobile phone to the new contact 14 renders the :new template 15 GET #edit 16 assigns the requested contact to @contact 17 renders the :edit template 18 POST #create 19 with valid attributes 20 creates a new contact 21 redirects to the new contact 22 with invalid attributes 23 does not save the new contact 7. Controller spec cleanup 79 24 re-renders the new method 25 PUT #update 26 valid attributes 27 located the requested @contact 28 changes @contact's attributes 29 redirects to the updated contact 30 invalid attributes 31 locates the requested @contact 32 does not change @contact's attributes 33 re-renders the edit method 34 DELETE destroy 35 deletes the contact 36 redirects to contacts#index 37 user access to contacts 38 behaves like public access to contacts 39 GET #index 40 populates an array of contacts 41 renders the :index view 42 GET #show 43 assigns the requested contact to @contact 44 renders the :show template 45 behaves like full access to contacts 46 GET #new 47 assigns a new Contact to @contact 48 assigns a home, office, and mobile phone to the new contact 49 renders the :new template 50 GET #edit 51 assigns the requested contact to @contact 52 renders the :edit template 53 POST #create 54 with valid attributes 55 creates a new contact 56 redirects to the new contact 57 with invalid attributes 58 does not save the new contact 59 re-renders the new method 60 PUT #update 61 valid attributes 62 located the requested @contact 63 changes @contact's attributes 64 redirects to the updated contact 65 invalid attributes 7. Controller spec cleanup 80 66 locates the requested @contact 67 does not change @contact's attributes 68 re-renders the edit method 69 DELETE destroy 70 deletes the contact 71 redirects to contacts#index 72 guest access to contacts 73 behaves like public access to contacts 74 GET #index 75 populates an array of contacts 76 renders the :index view 77 GET #show 78 assigns the requested contact to @contact 79 renders the :show template 80 GET #new 81 requires login 82 POST #create 83 requires login 84 PUT #update 85 requires login 86 DELETE #destroy 87 requires login Creating helper macros Now lets turn our attention to another bit of code weve used several times in our controllers. Whenever were testing what a logged-in user can or cant do, we simulate that login by setting a session value to a factory-generated users :id. Lets move this functionality to a macro in RSpec. Macros are an easy way to create methods which may be used across your entire test suite. Macros conventionally go into the spec/support directory as a module to be included in RSpecs configuration. First, heres a macro for setting that session variable: spec/support/login_macros.rb 1 module LoginMacros 2 def set_user_session(user) 3 session[:user_id] = user.id 4 end 5 end 7. Controller spec cleanup 81 Just a simple Ruby module and methodit accepts a user object and assigns session[:user_id] to that users own :id. Before we can use this new helper in our specs, weve got to let RSpec know where to find it. Inside the RSpec.configure block in spec/spec_helper.rb, well add the line config.include LoginMacros as shown below: spec/spec_helper.rb 1 Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} 2 3 RSpec.configure do |config| 4 # other RSpec configuration omitted ... 5 6 config.include LoginMacros 7 end Authentication options like Devise offer similar functionality. If youre using such a solution in your project, refer to its documentation for instructions on incorporating it into your test suite. With that in place, lets apply it to a controller spec. In a before block, well create a new admin user, then set the session to that userall in a single line: spec/controllers/contacts_controller_spec.rb 1 describe "admin access" do 2 before :each do 3 set_user_session create(:admin) 4 end 5 6 it_behaves_like "public access to contacts" 7 it_behaves_like "full access to contacts" 8 end It might seem silly to create a whole separate helper method for just one line of code, but in reality it could come to be that we change up our whole authentication system, and need to simulate login in a different fashion. By simulating login in this manner, we just have to make the change in one place. When we get to integration testing in the next chapter, this technique might help us reuse several lines of code, as we simulate each step of a user login. 7. Controller spec cleanup 82 Using custom RSpec matchers So far weve gotten a lot of mileage out of RSpecs built-in matchersand truth be told, you could probably test an entire application without ever straying from the standards. (I know; I have.) However, just as we reviewed when building some helper macros in the last section, adding a few custom matchers for your application can boost your test suites long-term reliability. In the case of our address book, what if we changed the route for the login form or where we direct users who try to access more than theyre allowed? As it is wed have a lot of examples to switch out to the new routeor we could set up a custom matcher and just change the route in one place. If you store custom matchers in spec/support/matchers, one matcher per file, the default RSpec configuration will automatically pick them up for use in your specs. Heres an example: spec/support/matchers/require_login.rb 1 RSpec::Matchers.define :require_login do 2 match do |actual| 3 redirect_to Rails.application.routes.url_helpers.login_path 4 end 5 6 failure_message_for_should do |actual| 7 "expected to require login to access the method" 8 end 9 10 failure_message_for_should_not do |actual| 11 "expected not to require login to access the method" 12 end 13 14 description do 15 "redirect to the login form" 16 end 17 end Lets take a quick tour of this code: the match block is what we expect to happenessentially, replacing the code after expect(something).to in a given spec. Note that RSpec doesnt load Rails UrlHelpers library, so well give the matcher a little help by calling its full path. Next we can provide some helpful messages to return if an example using the matcher doesnt passthe first one is a message for when we expect the matcher to return true, and the second for when it should return false. In other words, a single matcher covers both expect(:foo).to and expect(:foo).to_notno need to write two matchers. 7. Controller spec cleanup 83 Even though RSpecs syntax for expectations is moving fromshould to expect, custom matchers still use failure_message_for_should and failure_message_for_should_not. Now, replacing the matcher in our examples is easy: spec/controllers/contacts_controller_spec.rb 1 describe 'GET #new' do 2 it "requires login" do 3 get :new 4 expect(response).to require_login 5 end 6 end This is just one example of what we can do with custom matchers. Coming up with a fancier example at this point in our application would be contrived (and possibly confusing), so I suggest reviewing the sample code in the RSpec wiki. Note that you can pass a value into the matcher, making it more reusable. Summary If left unkempt, controller specs can sprawl out of control pretty quicklybut with a little manage- ment (and some help from RSpecs useful support methods) you can keep things in check for solid long-term maintenance. Just as you shouldnt ignore control specs, please also dont ignore your responsibility of keeping these specs pruned and tidy. Your future self will thank you for it. Weve spent a lot of time testing controllersbut as I said way back at the beginning of chapter 5, testing at this level is an economical way to build confidence in a large swath of your code base. By keeping these tests clean and readable, youll be sure to make that confidence last for the lifetime of your application. One more level of testing to go: Integration. The work weve done so far has given us comfort in our applications building blocksnext lets make sure they fit nicely together into a cohesive structure. Exercises Examine your own test suite and look for places to tidy up. Controller specs are a prime target, but check your model specs, too. What are the best methods for cleaning up each https://github.com/dchelimsky/rspec/wiki/Custom-Matchers 7. Controller spec cleanup 84 sectionshared examples? A custom matcher? A helper macro? Update your specs as needed, making sure they continue to pass along the way. Back in chapter 5, I mentioned that expect(@message.reload.is_inappropriate?).to be_- true could be streamlined with a custom matcher. What would that look like? 8. Feature specs So far weve added a good amount of test coverage to our contacts manager. We got RSpec installed and configured, set up some unit tests on models and controllers, and used factories to generate test data. Now its time to put everything together for integration testingin other words, making sure those models and controllers all play nicely with other models and controllers in the application. These tests are called feature specs in RSpec. Once you get the hang of them, they can be used to test a wide range of functionality within a Rails application. They may also be used to replicate bug reports from users. The good news is you know almost everything you need to know to write a solid feature specthey follow a similar structure youve been using in models and controllers, and you can use Factory Girl to generate test data for them. The star of the show, though, is Capybara, an extremely useful Ruby library to help define steps of a feature spec and simulate real-world use of your application. In this chapter, well look at the nuts and bolts of an RSpec feature spec: Well start with some thoughts on when and why feature specs make sense versus other options. Next well cover a few additional dependencies to aid in integration testing. Then well look at a basic feature spec. After that well tackle a slightly more advanced approach, with JavaScript requirements incorporated. Finally, well close with some discussion on best practices for feature specs. Check out the 08_features branch of the sample source to follow along with the code samples. Using the command line, type git checkout -b 08_features origin/08_features. See chapter 1 for additional details. Why feature specs? We just spent a lot of time going over controller testing. After all that, why are we doing another layer of tests? Because controller tests are relatively simple unit tests and, while they test an important component of your software, they are only testing a small part of an application. A feature spec covers more ground, and represents how actual users will interact with your code. 85 8. Feature specs 86 You may also be asking yourself, What about Cucumber? To be honest, Ive run hot and cold with Cucumber for a few years now. Its definitely got its usesbut its also got a lot of overhead and, unless you know how to use it correctly, can lead to brittle and ultimately useless tests. I can understand wanting to use Cucumber if youre working directly with a non-programmer product owner who doesnt want to look at a lot of code, but from my experience Capybaras DSL is understandable enough that non-programmers can still read through a feature spec and understand whats going on. And if youre not working with a non-programmer, then the extra overhead incumbent with Cucumber may not be worth the effort. Of course, Cucumber does have its ardent supporters. Its a staple in many development shops, so youll probably need to become familiar with it eventually, too. The good news is, if you do want or need to use Cucumber down the road, understanding how Capybara and RSpec work at the feature spec level will make things easier to understand. If you do go the Cucumber route, be mindful of any tutorial that existed prior to December, 2011. Thats when it was revealed that Cucumbers web_steps.rb filethe helpers that let you add steps like When I fill in "Email" with "[email protected]"were training wheels. Post-December 2011, its recommended that you make your Cucumber scenarios more direct, and leave the heavy lifting to custom step definitions using Capybara. Additional dependencies Way back in chapter 2, we included Capybara, DatabaseCleaner, and Launchy in our Gemfiles test group. If you havent added them yet, do so nowwere finally going to put them to use. Gemfile group :test do gem "faker", "~> 1.1.2" gem "capybara", "~> 2.0.2" gem "database_cleaner", "~> 0.9.1" gem "launchy", "~> 2.2.0" end https://github.com/jnicklas/capybara https://github.com/bmabey/database_cleaner http://rubygems.org/gems/launchy 8. Feature specs 87 Depending on your version of RSpec, you may need to do a little more configuration to use Capybara. The current version of RSpec (at this writing, version 2.13) only requires that you include them in your Gemfile, but if youre using an older version and run into problems, make sure youre requiring Capybara inside spec/spec_helper: 1 ENV["RAILS_ENV"] ||= 'test' 2 require File.expand_path("../../config/environment", __FILE__) 3 require 'rspec/rails' 4 require 'rspec/autorun' 5 require 'capybara/rspec' If at all possible, though, update to a more current version of RSpec. DatabaseCleaner will need some configuration regardless of your installed version of RSpec, but lets look at a simple spec that doesnt require DatabaseCleaner first. A basic feature spec Capybara lets you simulate how a user would interact with your application through a web browser, using a series of easy-to-understand methods like click_link, fill_in, and visit. What these methods let you do, then, is describe a test scenario for your app. Can you guess what this feature spec does? spec/features/users_spec.rb 1 require 'spec_helper' 2 3 feature 'User management' do 4 scenario "adds a new user" do 5 admin = create(:admin) 6 7 visit root_path 8 click_link 'Log In' 9 fill_in 'Email', with: admin.email 10 fill_in 'Password', with: admin.password 11 click_button 'Log In' 12 13 visit root_path 14 expect{ 8. Feature specs 88 15 click_link 'Users' 16 click_link 'New User' 17 fill_in 'Email', with: '[email protected]' 18 find('#password').fill_in 'Password', with: 'secret123' 19 find('#password_confirmation').fill_in 'Password confirmation', 20 with: 'secret123' 21 click_button 'Create User' 22 }.to change(User, :count).by(1) 23 expect(current_path).to eq users_path 24 expect(page).to have_content 'New user created' 25 within 'h1' do 26 expect(page).to have_content 'Users' 27 end 28 expect(page).to have_content '[email protected]' 29 end 30 end Walking through the steps of this spec, you should be able to see that the spec first creates a new administrator (a user who can create other users), then uses the login form to sign in as that administrator and create a new user using the same web form our applications administrators would use. This is an important distinction between feature specs and controller specsin controller specs, we bypass the user interface and send parameters directly to the controller method (which, in this case, would be multiple controllers and actionscontacts#index, sessions#new, users#new, and users#create). However, the results should be the samea new user is created, the application redirects to a listing of all users, a flash message is rendered to let us know the process was successful, and the new user is listed on the page. You may also recognize some techniques from previous chaptersfeature is used in place of describe to structure the spec, scenario describes a given example in place of it, and the expect{} Proc we checked out in chapter 5 plays the same role herewe expect that certain things will change when a user completes the scripted actions when interacting with the site. Notice the use of find('#password') and find('#password_confirmation) here. As you may guess, this method finds elements using whatever you pass into it as an argument. In this case its finding by CSS<div> elements by their ids. I could also find elements by XPath location, or just plain text as shown for click_link 'Users', fill_in 'Email', and so on. However, a spec will fail if a match is ambiguousin other words, if Id tried the following: fill_in 'Password', with: 'secret' fill_in 'Password confirmation', with: 'secret' Capybara would have returned an Ambiguous match error. If you receive such an error, fire up the view file rendering your HTML and look for alternative ways to locate the field you want to manipulate. (Prior to Capybara 2.0, such use of fill_in wouldnt have resulted in this error.) 8. Feature specs 89 If possible, I prefer to stick with plain text matchers, then CSS; if neither of those matches exactly what I want Ill defer to XPath-based matchers. Refer to Capybaras README file for more information. Following expect{}, we run a series of tests to make sure the resulting view is displayed in a way wed expect, using Capybaras not-quite-plain-English-but-still-easy-to-follow style. Check out, too, the within block used to specify where to look on a page for specific content in this case, within the <h1> tag in the index view for users. This is an alternative to the find() approach used to locate the password and password confirmation fields. You can get pretty fancy with this if youd likemore on that in just a moment. One final thing to point out here: Within feature specs, its perfectly reasonable to have multiple expectations in a given example or scenario. Feature specs typically have much greater overhead than the smaller examples weve written so far models and controllers, and as such can take a lot longer to set up and run. You may also add expectations mid-test. For example, in the previous spec I may want to verify that the user is notified of successful login via a flash messagethough in reality, such an expectation might be more appropriate in a feature spec dedicated to nuances of our applications login mechanism. Changes in Capybara 2.0 Capybara 2.0 introduced a few changes to the DSL, including the aforementioned use of the term feature instead of request. Request specs still have a place, but are now intended to test any public API your application might serve. In addition to moving the location of these specs, Capybara 2.0 introduced a few aliases to help feature specs feel a little more like acceptance tests written in other frameworks (read: Cucumber). These aliasesnamely, the aforementioned feature and scenarioare exclusive to feature specs. Other aliases include background for before and given for let (which well cover in chapter 9). A couple of caveats: While tests at other levels of your app can nest describe blocks within other describe blocks, you cant nest feature blocks. Youll also likely encounter errors if you mix and match syntax. You can have multiple features per file, and multiple scenarios per feature, just as you can have multiple it blocks within a describe. Strictly speaking, you can use describe and it in your feature specs, but for best results, I recommend using the new Capybara DSL. Thats how well write our examples for our address book application moving forward. Adding feature specs The quickest way to add a new feature spec to your application is to create a new file inside spec/features, beginning with the following template: 8. Feature specs 90 1 require 'spec_helper' 2 3 feature 'my feature' do 4 background do 5 # add setup details 6 end 7 8 scenario 'my first test' do 9 # write the example! 10 end 11 end As of this writing, if using Rails scaffold generator to create models and their associated controllers, views, migrations, and specs, the corresponding feature spec will be added to spec/requests. Delete it, or move it to spec/features and edit. You can also make sure the scaffold generator doesnt create these files for you by making sure request_specs: false is included in your application.rb files RSpec generator configuration. Debugging feature specs Ive already mentioned that its typical to see a given scenario in a feature have multiple expectations. However, that can sometimes lead you to wonder why a scenario might be failing at a certain point. For the most part, you can use the same tools you use to debug any Ruby application within RSpecbut one of the easiest to use is Launchy. Launchy does just one thing when called: It saves the feature specs current HTML to a temporary file and renders it in your default browser. To use Launchy in a spec, add the following line anywhere youd like to see the results of the previous step: save_and_open_page For example, in the feature spec shown earlier in this chapter, I could use Launchy to look at the results of my new user form: 8. Feature specs 91 spec/features/users_spec.rb 1 require 'spec_helper' 2 3 feature 'User management' do 4 scenario "adds a new user" do 5 admin = create(:admin) 6 sign_in admin 7 8 visit root_path 9 expect{ 10 click_link 'Users' 11 click_link 'New User' 12 fill_in 'Email', with: '[email protected]' 13 find('#password').fill_in 'Password', with: 'secret123' 14 find('#password_confirmation').fill_in 'Password confirmation', 15 with: 'secret123' 16 click_button 'Create User' 17 }.to change(User, :count).by(1) 18 19 save_and_open_page 20 21 # remainder of scenario 22 end 23 end Remove the save_and_open_page line, of course, when you dont need it anymore. That one line has saved me untold hours of headache in my own specs. A little refactoring Before we move on, lets take another look at that feature spec for creating new users. Theres at least one thing we can refactor. As you may recall, in chapter 7 we extracted the simulated user login into a helper macro. We can do the same thing for feature specs. Why not just use the same technique weve used in controller specs? Because, at the feature level, were testing that things work the way users would interact with them. This includes logging in! However, that doesnt mean we cant extract the login steps into a helper. Lets do that now: 8. Feature specs 92 spec/helpers/login_macros.rb 1 module LoginMacros 2 # controller login helper omitted ... 3 4 def sign_in(user) 5 visit root_path 6 click_link 'Log In' 7 fill_in 'Email', with: user.email 8 fill_in 'Password', with: user.password 9 click_button 'Log In' 10 end 11 end And we can use the helper in our feature spec like this: spec/features/users_spec.rb 1 feature 'User management' do 2 scenario "adds a new user" do 3 admin = create(:admin) 4 sign_in admin 5 6 # remaining steps omitted ... 7 end 8 end Including JavaScript interactions So weve verified, with a passing spec, that our user interface for adding contacts is working as planned. Lets get slightly more complicated, testing a new (albeit contrived) version of the application. This feature allows us to show or hide the administrator-only links on a given pagethat is, links to our controllers non-read methods. To accomplish this, well use a simple jQuery toggle function. The spec looks something like this: 8. Feature specs 93 spec/features/contacts_spec.rb 1 feature "Manage contacts" do 2 background do 3 user = create(:user) 4 sign_in(user) 5 end 6 7 # other examples omitted ... 8 9 scenario "edits a contact and displays the updates" do 10 contact = create(:contact, firstname: 'Sam', lastname: 'Smith') 11 12 visit root_path 13 14 within "#contact_#{contact.id}" do 15 expect(page).to_not have_content 'Edit' 16 end 17 18 click_link 'Toggle Admin' 19 within "#contact_#{contact.id}" do 20 click_link 'Edit' 21 end 22 fill_in 'Firstname', with: 'Samuel' 23 fill_in 'Lastname', with: 'Smith, Jr.' 24 fill_in 'Email', with: '[email protected]' 25 fill_in 'home', with: '123-555-1234' 26 fill_in 'work', with: '123-555-3333' 27 fill_in 'mobile', with: '123-555-7777' 28 click_button 'Update Contact' 29 30 expect(page).to have_content 'Contact was successfully updated' 31 expect(page).to have_content 'Samuel Smith, Jr.' 32 expect(page).to have_content '[email protected]' 33 expect(page).to have_content '123-555-1234' 34 expect(page).to have_content '123-555-3333' 35 expect(page).to have_content '123-555-7777' 36 end 37 end While were here, lets take a quick look at line 13. Notice that the DOM for our view (in this case, app/views/contacts/index.html.erb) includes a specific ID for each contacts row. Again, this is 8. Feature specs 94 for the sake of Capybara and avoiding ambiguitywe need to be clear which Edit link to click. This requires some code to be present in the view itself, using the dom_id() helper method built into Rails itself: app/views/contacts/index.html.erb 1 <% @contacts.each do |contact| %> 2 <tr id="<%=dom_id(contact) %>"> 3 <!-- etcetera --> Otherwise, nothing too complexbut theres a problem. As-is, were running the feature spec using Capybaras default web driver. This driver, Rack::Test, cant do JavaScript, so it ignores it. Luckily, Capybara bundles support for the Selenium web driver out of the box. With Selenium, you can simulate more complex web interactions, including JavaScript, through a copy of Firefox loaded on your computer. Selenium makes this possible by running your test code through a lightweight web server, and automating the browsers interactions with that server. Heres what the modified spec looks like: spec/features/contacts_spec.rb 1 feature "Manage contacts" do 2 background do 3 user = create(:user) 4 sign_in(user) 5 end 6 7 # other examples omitted ... 8 9 scenario "edits a contact and displays the updates", js: true do 10 contact = create(:contact, firstname: 'Sam', lastname: 'Smith') 11 12 visit root_path 13 14 within "#contact_#{contact.id}" do 15 expect(page).to_not have_content 'Edit' 16 end 17 18 click_link 'Toggle Admin' 19 within "#contact_#{contact.id}" do 20 click_link 'Edit' 8. Feature specs 95 21 end 22 fill_in 'Firstname', with: 'Samuel' 23 fill_in 'Lastname', with: 'Smith, Jr.' 24 fill_in 'Email', with: '[email protected]' 25 fill_in 'home', with: '123-555-1234' 26 fill_in 'work', with: '123-555-3333' 27 fill_in 'mobile', with: '123-555-7777' 28 click_button 'Update Contact' 29 30 expect(page).to have_content 'Contact was successfully updated' 31 expect(page).to have_content 'Samuel Smith, Jr.' 32 expect(page).to have_content '[email protected]' 33 expect(page).to have_content '123-555-1234' 34 expect(page).to have_content '123-555-3333' 35 expect(page).to have_content '123-555-7777' 36 end 37 end Notice whats different: Weve added js: true to the example, to tell Capybara to use a JavaScript- capable driver (Selenium, by default). We also need to configure Database Cleaner to help with database transactions in our tests. First, change RSpecs default settings for database transactions, and tell it to use DatabaseCleaners :truncation method when running specs through the Selenium driver. Lets make the following changes to spec/spec_helper.rb: spec/spec_helper.rb 1 RSpec.configure do |config| 2 3 # earlier configurations omitted ... 4 5 # Set config.use_transactional_fixtures to false 6 config.use_transactional_fixtures = false 7 8 config.before(:suite) do 9 DatabaseCleaner.strategy = :truncation 10 end 11 12 config.before(:each) do 13 DatabaseCleaner.start 14 end 15 16 config.after(:each) do 8. Feature specs 96 17 DatabaseCleaner.clean 18 end Second, we need to monkey patch ActiveRecord to use threads. Add an additional file in spec/support with the following alterations to ActiveRecord::Base: spec/support/shared_db_connection.db 1 class ActiveRecord::Base 2 mattr_accessor :shared_connection 3 @@shared_connection = nil 4 5 def self.connection 6 @@shared_connection || retrieve_connection 7 end 8 end 9 ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection Why is this necessary? The short answer is its due to how Selenium handles database transactions. We need to share data state across the Selenium web server and the test code itself. Without DatabaseCleaner and the above patch, were apt to get sporadic error messages resulting from tests not properly cleaning up after themselves. For a more complete description of this setup, check out Avdi Grimms Virtuous Code blog. Thank you to reader Chris Peters for pointing this fix out to me. With those changes, the feature spec will run through Firefox, and youre one step closer to a well- tested application. Capybara drivers So far, weve put two drivers to use in our feature specs. The default driver, RackTest, is a reliable solution for testing basic browser interactions. Its headless, so these interactions are all simulated in the background. Selenium is provided for more complicated interactions, including those requiring JavaScript or redirections (including redirections away from your application). Seleniums added functionality comes at a price, howeveryoull no doubt tire of waiting for Firefox to launch and run your specs every time, especially as your test suite grows. Fortunately 8. Feature specs 97 there are headless options supporting JavaScript. Two popular headless drivers for Capybara include capybara-webkit and Poltergeist. Note that both of these drivers may require additional dependencies and can take some time to set up, but if your application has a lot of feature specs requiring more than the basics offered by RackTest it will be worth the extra setup time. Refer to Capybaras README for details on setting up alternate drivers. Summary This wasnt a long chapterespecially compared to the amount of time spent on controllersbut it does introduce a lot of new concepts and build upon what youve learned so far. In fact, its brevity can be attributed to the fact that it simply builds upon skills you acquired in the previous six chapters. Review it a few times if you have to, and keep practicing. If you get stuck, its not against the rules to fire up your web browser and see if what youre expecting to happen in your tests is actually happening in the browser. (Launchy can help with this, too.) At this point youve been exposed to the key tools and techniques youll use to test your Rails applications. Weve still got a few things to cover before we wrap up, though. In the next chapter well look at techniques to help keep your growing test suite running as quickly as possible. Exercises Write some feature specs and make them pass! Start with simple user activities, moving on to the more complex as you get comfortable with the process. As you did with your controller specs, use this time to note places where your code could use refactoring. Again, if your app requires you to do a lot of setup to get everything just right for a test, its a sign that you could be simplifying things in your code base. Clean up your code and run your feature specs again. Do they still pass? As you write the steps required for a given feature example, think about your userstheyre the ones who work through these steps in their browsers when they need to get something done. Are there steps you could simplifyor even removeto make the overall user experience for your application more pleasant? https://github.com/thoughtbot/capybara-webkit https://github.com/jonleighton/poltergeist https://github.com/jnicklas/capybara 9. Speeding up specs Back in chapter 7, we did a round of refactoring on controller specs to make them easier to read and maintain. Specifically, we accomplished three tasks: We reduced redundancy with shared examples, moved frequently-used functionality into helper macros, and built custom matchers to simplify expectations within our examples. Now that weve got a relatively complete test suite, lets look again at how we can refactorbut this time for speed. By speed I mean two things: One, of course, is the amount of time it takes your specs to run. Our little apps tests are already getting on the slow side. As it growsassuming the test suite grows with itthat will change. The goal is to keep it manageable, without sacrificing the readability afforded us by RSpec. The second thing I mean by speed is howquickly you as a developer can create meaningful, clear specs. Well touch on both of these aspects in this chapter. Specifically, well cover: RSpecs options for terse, but clean, syntax for shorter specs. Simplified specs with Shouldas custom matchers. More efficient data for testing with mocks and stubs. Using tags to filter out slow specs Automating test execution and preloading Rails. Techniques for speeding up the suite as a whole. Check out the 08_features branch of the sample source to follow along with the code samples. Using the command line, type git checkout -b 09_speedup origin/09_speedup. See chapter 1 for additional details. Optional terse syntax One critique of our specs so far might be that theyre pretty wordyweve been following some best practices and providing clear labels for each test, and one expectation per example. Its all been on purpose. However, RSpec provides techniques to continue these best practices while reducing your keystrokes. They may be used together to really streamline things, or individually to clean up longer-form specs. 98 9. Speeding up specs 99 let() So far weve been using before :each blocks to assign frequently-used test data to instance variables. An alternative to this, preferred by many RSpec users, is to use let(). let() gives us two advantages: 1. It caches the value without assigning it to an instance variable 2. It is lazily evaluated, meaning that it doesnt get assigned until a spec calls upon it Heres how we can use let() in a controller spec: spec/controllers/contacts_controller_spec.rb 1 require 'spec_helper' 2 3 describe ContactsController do 4 let(:contact) do 5 create(:contact, firstname: 'Lawrence', lastname: 'Smith') 6 end 7 8 # rest of spec file omitted ... Then, instead of working with the contact via @contact, we can just use contact like so: spec/controllers/contacts_controller_spec.rb 1 describe 'GET #show' do 2 it "assigns the requested contact to contact" do 3 get :show, id: contact 4 expect(:contact).to eq contact 5 end 6 7 it "renders the :show template" do 8 get :show, id: contact 9 expect(response).to render_template :show 10 end 11 end However, this causes a problem in the example testing whether the controllers destroy() method actually deletes data from persistence. Heres the currently failing spec: 9. Speeding up specs 100 spec/controllers/contacts_controller_spec.rb 1 describe 'DELETE destroy' do 2 it "deletes the contact" do 3 expect{ 4 delete :destroy, id: contact 5 }.to change(Contact,:count).by(-1) 6 end 7 end The count doesnt change, because the example doesnt know about contact until after were in the expect{} Proc. To fix this, well just call contact before the Proc: spec/controllers/contacts_controller_spec.rb 1 describe 'DELETE destroy' do 2 it "deletes the contact" do 3 contact 4 expect{ 5 delete :destroy, id: contact 6 }.to change(Contact,:count).by(-1) 7 end 8 end We could also use let!() (note the exclamation mark!), which forces contact to be assigned prior to each example. Or we could include let() within a before blockwhich may begin to defeat the purpose of using let() to begin with. subject{} subject{} lets you declare a test subject, then reuse it implicitly in any number of subsequent examples. Read on to see it in action. it{} and specify{} it{} and specify{} are synonymousthey are simple blocks that wrap an expectation. Weve been using it{} since chapter 3, in a longer form. In other words, you could change 9. Speeding up specs 101 subject { build(:user, firstname: 'John', lastname: 'Doe') } it 'returns a full name' do should be_named 'John Doe' end to subject { build(:user, firstname: 'John', lastname: 'Doe') } it { should be_named 'John Doe' } And get the same results. Trivial here, perhaps, but as specs grow these one-liners can make a difference. Note, too, that even though weve been using the expect syntax in tests, these one- liners still use should. This is by design: As mentioned by RSpecs developers, should reads better in these examples. Read your specs aloud as you write them, and use the term that makes the most sensethere are not hard rules about when to use one or the other. Shoulda Shoulda is an extensive library of helpers to make testing common functionality a breeze. By including one additional gem, we can reduce some of our specs from three or four or five lines down to one or two. subject(), it{} and specify{} really shine when used in conjunction with the shoulda-matchers gem. Include shoulda-matchers in the :test group of your Gemfile, and youll automatically have access to a number of useful matchersfor example: subject{ Contact.new } specify { should validate_presence_of :firstname } Nice and readable, with a good amount of coverage. We can also apply our own custom matchers to streamline even more. For example, the following custom matcher: http://rubygems.org/gems/shoulda 9. Speeding up specs 102 spec/models/contact_spec.rb 1 RSpec::Matchers.define :be_named do |expected| 2 match do |actual| 3 actual.name eq expected 4 end 5 description do 6 "return a full name as a string" 7 end 8 end Can easily be called with the following it{} block: it { should be_named 'John Doe' } Yes, this example might be overkill, but hopefully it gives you an idea of the different ways you can streamline your specswithout sacrificing readability: Contact should return a full name as a string should have 3 phones should require firstname to be set should require lastname to be set And so on. Mocks and stubs Mocking and stubbing and the concepts behind them can be the subjects of lengthy chapters (if not whole books) of their own. Search them online and youll inevitably come to an occasionally contentious debate on the right and wrong ways to use them. Youll also find any number of people attempting to define the two termsto varying degrees of success. My best definitions of each: A mock is some object that represents a real object, for testing purposes. These are also known as test doubles. These are sort of what weve using Factory Girl to accomplish, with the exception that a mock doesnt touch the databaseand thus takes less time to set up in a test. 9. Speeding up specs 103 A stub overrides a method call on a given object and returns a predetermined value for it. In other words, a stub is a fake method which, when called upon, will return a real result for use in our tests. Youll commonly use this to override the default functionality for a method, particularly in database or network-intensive activity. Here are a couple of loose examples: To create a mock contact, you can use the Factory Girl build_stubbed() method to generate a fully-stubbed fake, knowing how to respond to various methods like firstname, lastname, and fullname. It does not, however, persist in the database. To stub a method in the Contact model itself, youd use a stub along the lines of Contact.stub(:order).with('lastname,firstname').and_- return([contact]). In this case, were overriding the order scope on the Contact model. We pass a string to specify the SQL order (in this case, the lastname and firstname fields), then tell it what we want backa single-element array containing a contact we presumably created earlier in the spec. Youre more likely to find RSpecs built-in mocking libraries or an external library like Mocha used in projects, or one of a number of other options available. For the sake of a beginners perspective here, they all operate similarly, albeit with tradeoffs. It may make more sense to view these in the context of a controller spec. spec/controllers/users_controller_spec.rb 1 describe 'GET #show' do 2 let(:contact) { build_stubbed(:contact, 3 firstname: 'Lawrence', lastname: 'Smith') } 4 5 before :each do 6 Contact.stub(:persisted?).and_return(true) 7 Contact.stub(:order).with('lastname, firstname').and_return([contact]) 8 Contact.stub(:find).with(contact.id.to_s).and_return(contact) 9 contact.stub(:save).and_return(true) 10 end 11 12 before :each do 13 Contact.stub(:find).with(contact.id.to_s).and_return(contact) 14 get :show, id: contact 9. Speeding up specs 104 15 end 16 17 it "assigns the requested contact to @contact" do 18 expect(assigns(:contact)).to eq contact 19 end 20 21 it "renders the :show template" do 22 expect(response).to render_template :show 23 end 24 end Walking through the spec, we first use let() to assign a stubbed mock contact to contact. Then, we add some stubbed methods to both the Contact model and the contact instance. Since the controller will expect both Contact and contact to respond to several ActiveRecord methods, we need to stub the methods well be using in the actual controller, returning what wed expect ActiveRecord to provide back to the controller. Finally, we use it blocks for the examples themselves, as weve been doing throughout the book. In this case, though, all of our test data are based on mocks and stubs, and not actual calls to the database or the Contact model itself. On the plus side, this example is more isolated than specs weve written previouslyits only concern is the controller method in question; it doesnt care about the model layer or the database or anything else. On the down side, this isolation is leading to additional code (and questionable readability) in the specs. With all that said, if you dont want to mess with mocks and stubs too much, dont worryyou can go a long way with using Ruby objects for basic stuff, and factories for more complex setups, as we have throughout this book. Stubs can also get you into trouble, as noted in the updated PeepCode series on RSpec. One could easily stub out important functionality, resulting in tests that, well, dont actually test anything. Unless things get very slow, or you need to test against data that is difficult to recreate (such as an external API or other web service, which well cover in a bit more practical tones in the next chapter) then objects and factories may be all you need. Automation with Guard and Spork Forgetting to run specs early and often can result in lots of lost time. If you dont realize theres an issue somewhere, and keep piling new code on top of that issue, you may waste valuable minutesor even hours. But switching to a terminal and running rspec from the command line can get tedious (and chip away at our time, too). Guard to the rescue! https://peepcode.com/screencasts/rspec 9. Speeding up specs 105 Guard watches files you specify, and does things based on what it sees. In our case, we want it to watch files in our app and spec directories, and run the relevant specs when those files change. For example, if I make a change to app/models/contact.rb, then spec/models/contact_spec.rb should run. If it fails, it should keep running until it passes. To use Guard, first make sure guard-rspec included in your Gemfiles :test and :development groups (see chapter 2). guard-rspec will include Guard itself. Then create a Guardfile from the command line: bundle exec guard init rspec This will generate a Guardfile in your Rails applications root, serving as Guards configuration for your app. Its pretty useful out of the box, but youll probably want to tweak it to your own preferences. I typically set the following: notification: false: I prefer to keep an eye my specs running on a terminal window instead of receiving pop-ups messages. all_on_start: false and all_on_pass: false: Ive been doing this for awhile; I know to run my full test suite before committing any changes Ive made. If I want to run my specs at any time after firing up Guard I can just press return. Same with running all specs upon passing; I like having control of the situation. Run feature specs upon changes to views: Since I avoid RSpec view specs, I rely on Capybara feature specs to test this layer of my apps. Generally speaking, I dont run feature specs when changing my models or controllers. As with anything, though, it depends on the situation. The generated Guardfile doesnt particularly lend itself to display in a book format. See the Guardfile in the sample source to see it more like it would look in an application. Run bundle exec guard to get things going. Guard will run your full test suite, then dutifully observe for changes and run specs as needed. You can add other options as wellfor example, I sometimes prefer to only run the full test suite on demand. The following additions to the Guardfile accomplish this: https://github.com/guard/guard 9. Speeding up specs 106 Guardfile 1 guard 'rspec', :version => 2, :cli => '--color --format documentation', 2 :all_on_start => false, :all_after_pass => false do Guards not just for watching and running your specs. It can streamline CSS compilation, run Cucumber features, run code metrics, reboot development servers, and more. Check out a full list of Guards on GitHub. For more on Guard, check out the Railscasts episode on the topic. Once our tests get started, they run pretty quickly. However, at this point weve got a lag each time we start a test runthe lag caused by the Rails application needing to spin up each time. With Spork, we can limit the lag to just the first time we fire up the test suiteafter that, specs will run with much more immediacy. Combined with Guard, Spork is one of the better ways to reduce your testing time. Theres already an excellent Railscasts episode on Spork so Im not going to cover it too in- depth here. However, you can see my Spork configuration in this chapters sample source (refer to Guardfile and spec/spec_helper.rb). Be aware, since the coverage of Spork on Railscasts, Rails- specific features have been extracted to the spork-rails gem. You may also be interested in some newer solutions to this problem like Zeus, Commands, and the promising Spring. Like Spork, each has its own strengths and drawbacksIve stuck with Spork here due to its longevity, relative ease of setup, and overall compatibility with various operating systems and Ruby versions. Tags Whether or not you opt to add Guard to your workflow, RSpecs tags feature can help you fine-tune which specs to run at a given time. To apply a tag, add it to a given example: it "processes a credit card", focus: true do # details of example end You can then run only the specs with the focus tag from the command line: https://github.com/guard http://railscasts.com/episodes/264-guard http://railscasts.com/episodes/285-spork https://github.com/sporkrb/spork-rails https://github.com/burke/zeus https://github.com/rails/commands https://github.com/jonleighton/spring https://www.relishapp.com/rspec/rspec-core/v/2-4/docs/command-line/tag-option 9. Speeding up specs 107 $ bundle exec rspec . --tag focus You can also configure RSpec to only run (or never run) examples with specific tags; for example: spec/spec_helper.rb RSpec.configure do |c| c.filter_run focus: true c.filter_run_excluding slow: true end This is particularly useful when using Guard, as you can turn a given tag on or off in your spec_- helper.rb file, allow Guard to reload itself, and keep working. I dont use this feature often, but find it invaluable when I need it. Other speedy solutions Remove unnecessary tests If a test has served its purpose, and youre confident you dont need it for regression testing, delete it. If you do want to hold onto it for some reason, mark it as a pending spec: 1 it "loads a lot of data" do 2 pending "no longer necessary" 3 # your spec's code; it will not be run 4 end I recommend this over commenting out the testsince pending specs are still listed when you run the test suite youll be less apt to forget theyre there. That said, I ultimately recommend just deleting the unnecessary codebut only when youre comfortable doing so. Take Rails out of the equation The changes weve made above will all play a part in reducing the amount of time it takes the suite to run, but ultimately one of the biggest slowdowns is Rails itselfwhenever you run tests, some or all of the framework needs to be fired up. If you really want to speed up your test suite, you can go all out and remove Rails from the equation entirely. Whereas Spork still loads the frameworkbut limits itself to loading oncethese solutions go one step further. 9. Speeding up specs 108 This is a little more advanced than the scope of this book, as it requires a hard look at your applications overall architecture. It also breaks a personal rule I have when working with newer Rails developersthat is, avoid breaking convention whenever possible. If you want to learn more, though, I recommend checking out Corey Haines talk on the subject and the Destroy All Software screencast series from Gary Bernhardt. Summary We looked at some pretty weighty topics in this chapter. Up until now, I didnt talk about varying techniques to get the testing job donebut now youve got options. You can choose the best way for you and your team to provide clear documentation through your specseither by using a verbose technique as we did in chapter three, or in a more terse fashion as shared here. You can also choose different ways to load and work with test datamocks and stubs, or factories, or basic Ruby objects, or any combination thereof. Finally, you now know a few different techniques for loading and running your test suite. Youre on your way to making RSpec your own. Were in the home stretch nowjust a few more things to cover, then well wrap up with some big picture thinking on the testing process in general and how to avoid pitfalls. First, lets look at some of the corners of a typical web application we havent tested yet. Exercises Find specs in your suite that could be cleaned up with let(), subject{}, and it{}. By how much does this exercise reduce your specs footprint? Is it still readable? Which methodterse or verbosedo you prefer? (Hint: Theres really no right answer for that last question.) Install shoulda-matchers in your application and find places you can use it to clean up your specs (or test things you havent been testing). Look into the gems source on GitHub to learn about all of the matchers Shoulda offers to RSpec users. Using RSpec tags, identify your slow specs. Run your test suite including and excluding the tests. What kind of performance gains do you see? http://confreaks.com/videos/641-gogaruco2011-fast-rails-tests https://www.destroyallsoftware.com/screencasts 10. Testing the rest This chapter is still being edited and has not been updated to reflect the latest gem versions for our testing environment. At this point weve got decent coverage across the address book application. Weve tested our models and controllers, and also tested them in tandem with views via request specs. For this basic application we should be covered pretty well with these core testing techniques. However, many Rails applications (including yours, probably) arent this simple. Maybe your app sends email to users, or interacts with an external web service, or handles file uploads. Maybe it performs certain functionality based on the date or time. We can test these facets, too! In this chapter well survey: How to test for email delivery. How to test file uploads. Manipulating the time within specs. Testing against external web services. Testing rake tasks. No code samples for this chaptereverything I tried to come up with as a feature for the address book seemed contrived, though Im not ruling out some code samples for a future release of the book. Testing email delivery Testing that your applications mailers are doing their job is relatively easyall it takes is another gem and a few more lines of configuration. The gem is Email Speca useful set of custom matchers to test a given messages recipients, subject, headers, and content. Once youve added the gem to your Gemfiles :test group and run bundle install, youll just need to add a few more configuration lines to spec/spec_helper.rb: http://rubygems.org/gems/email_spec 109 10. Testing the rest 110 spec/spec_helper.rb 1 require "email_spec" 2 config.include(EmailSpec::Helpers) 3 config.include(EmailSpec::Matchers) With these lines you may now add expectations like the following: # some setup done to trigger email delivery ... open_last_email.should be_delivered_from sender.email open_last_email.should have_reply_to sender.email open_last_email.should be_delivered_to recipient.email open_last_email.should have_subject message.subject open_last_email.should have_body_text message.message Where open_last_email is a helper that opens the most recently-sent email and gives you access to its attributes. As outlined in the librarys documentation, you can also create a new mail object and work directly with it: email = MessageMailer.create_friend_request("[email protected]") email.should deliver_to("[email protected]") email.should have_subject "Friend Request" As you can see, the custom matchers provided by Email Spec are nice and readablesee a complete list of of matchers in the documentation. While there, take a look at the list of helpers made available to you. I like to use open_last_email in particular when testing mail delivery at the integration (request) level. In general, Email Spec works great at the model and controller levels as welluse it where it makes the most sense within your application. For more on Email Spec and testing mail delivery in general, I defer once again to the Railscasts episode, How I Test. Its required viewing, as far as Im concerned, for anyone wanting to get better with testing mail in RSpec. http://rubydoc.info/gems/email_spec/1.2.1/frames 10. Testing the rest 111 Testing file uploads Making sure file uploads worked the way Id intended was a sticking point in my testing routine for a long time. In particular, how does a fake file get included into a spec? Where does it get stored in the meantime? Even though Rails provides a means of uploading files from your fixtures directory, Ive found it to be hit or miss and tend to use this straightforward method. Place a small, dummy file (ideally representative of your real-world data) in your spec/factories directory. Then you can refer to it in a factory like so: 1 FactoryGirl.define do 2 factory :user do 3 sequence(:username) { |n| "user#{n}"} 4 password 'secret' 5 avatar { File.new("#{Rails.root}/spec/factories/avatar.png") } 6 end 7 end If your model requires the attached file to be present, youll probably want to make sure it gets stored when using a factory to generate test data. More importantly, you can also access the file explicitly in specs, such as the following request example: 1 it "creates a new user" do 2 visit new_user_url 3 fill_in 'Username', with: 'aaron' 4 fill_in 'Password', with: 'secret' 5 attach_file 'Avatar', 6 File.new("#{Rails.root}/spec/factories/avatar.png") 7 click_button 'Sign up' 8 User.last.avatar_file_name.should eq 'avatar.png' 9 end Using the factory above, we can also test this at the controller level like this: 10. Testing the rest 112 1 it "uploads an avatar" do 2 post :create, user: create(:user) 3 assigns(:user).avatar_file_name.should eq 'avatar.png' 4 end Im going to continue to refine this and will update the book when Ive found a better solution that works for me. Testing the time What if your application has expectations based on the time or date? For example, say we want to wish visitors a Happy New Year when they visit our site, but only on January 1. We can use Timecop to freeze time, making it possible to test such things without resorting to heavy-duty Ruby date manipulation. All you need to do is include the Timecop gem in your Gemfile, then use it like I am in this hypothetical request spec: 1 it "wishes the visitor a Happy New Year on January 1" do 2 Timecop.travel Time.parse("January 1") 3 visit root_url 4 page.should have_content "Happy New Year!" 5 Timecop.return 6 end Take note of the call to Timecop.return in these code samples. This resets the clock to your systems time and helps RSpec properly report the amount of time your tests take to run. Timecop is also useful in situations where you need to impose a deadlinefor example, maybe you need to make sure people have filed their taxes on time: http://rubygems.org/gems/timecop 10. Testing the rest 113 1 it "doesn't allow taxpayers to file after April 15" do 2 Timecop.travel Time.parse("April 16") 3 visit tax_submit_form 4 page.should have_content "Sorry, you're too late!" 5 Timecop.return 6 end or 1 it "gives taxpayers up until the 15th to file" do 2 Timecop.travel Time.parse("April 15") 3 visit tax_submit_form 4 page.should have_content "There's still time to file, but hurry!" 5 Timecop.return 6 end Thats how I usually use Timecop, but another common usage is when you want to stop time during the test. For example, maybe you want to be really sure Rails default timestamps are working. You could do something like this in a model spec: 1 it "stamps the model's created at with the current time" do 2 Timecop.freeze 3 user = create(:user) 4 user.created_at.should eq Time.now 5 Timecop.return 6 end Without Timecop.freeze in the example, the split-second difference between when the data was persisted and when the spec checks its value would be just enough to cause it to fail. Testing web services Full disclosure: I have to test an applications interaction with web services very infrequently. Most of the work I do in my day job is fairly isolated from the rest of the world. However, I have worked on a handful of projects requiring things like credit card processing, and can tell you that while its technically possible to test these at the integration level, using techniques Ive already shared, its slow and prone to errors. Its far better to stub out these services. Luckily, there are a few utilities that make this very easy. Allow me to direct you to a couple more Railscasts: First is a free episode on using Fakeweb toyou guessed itfake web requests without having to write complex stubs. The second episode requires http://railscasts.com/episodes/276-testing-time-web-requests 10. Testing the rest 114 a Railscast Pro subscription, but is essential if you need to test against web services. VCR works atop an HTTP library (such as Fakeweb) and makes recording and reusing web service interactions incredibly simple and fast. (The VCR documentation is also pretty solid, if youd prefer a free resource.) Testing rake tasks If youve been developing in Rails for awhile youve probably written at least one Rake command line utility for your application. I often use Rake tasks for things like legacy data transfers or scheduled operations. Legacy transfers in particular can get pretty gnarly, so just like the rest of my code I like to build tests to make sure I wont get any surprises. In my experience, the best way to do this is to abstract any code youve got in a given Rake task into a class or module, then call that method within the task. For example, imagine that weve got a Rake task to move information from a legacy Person class to the Contact class weve been using throughout the book. A procedural approach to this might look something like this: 1 namespace :legacy do 2 desc "Move Persons to Contacts" 3 task person: :environment do 4 Person.all.each do |person| 5 Contact.create!( 6 firstname: person.firstname, 7 lastname: person.lastname, 8 email: person.email 9 ) 10 end 11 end 12 end In this case, I might create a Legacy class lib/legacy.rb and move the bulk of the task to a class method within it: http://railscasts.com/episodes/291-testing-with-vcr https://www.relishapp.com/myronmarston/vcr/docs 10. Testing the rest 115 1 class Legacy 2 def self.move_people 3 Person.all.each do |person| 4 Contact.create!( 5 firstname: person.firstname, 6 lastname: person.lastname, 7 email: person.email 8 ) 9 end 10 end 11 end And update my original Rake task: 1 namespace :legacy do 2 desc "Move Persons to Contacts" 3 task person: :environment do 4 Legacy.move_people 5 end 6 end Now I can easily test the task by testing the Legacy class. To mirror the application structure, lets first create the directory spec/lib, then add legacy_spec.rb to it and test: 1 require 'spec_helper' 2 3 describe Legacy do 4 it 'creates a contact from a person' 5 # etc. 6 end You can use the same techniques weve covered throughout the book to test Rake-related code just like you would any other code in your application. Summary Even though things like email, file uploads, timestamps, web services, and utility tasks may be on the fringes of your application, take the time to test them as neededbecause you never know, one day that web service may become more central to your apps functionality, or your next app may rely heavily on email. Theres never a bad time to practice, practice, practice. 10. Testing the rest 116 You now know how to test everything I test on a regular basis. It may not always be the most elegant means of testing, but ultimately it provides me enough coverage that I feel comfortable adding features to my projects without the fear of breaking functionalityand if I do break something, I can use my tests to isolate the situation and fix the problem accordingly. As we wind down our discussion of RSpec and Rails, Id like to talk about how to take what you know and use it to develop software in a more test-driven fashion. Thats what well cover in the next chapter. Exercises If your application has any mailer functionality, get some practice testing it now. Common candidates might be password reset messages and notifications. Does your application have any file upload functionality or time-sensitive functions? Again, its a great idea to practice testing these functions, using the utilities shared in this chapter. Its easy to forget about these requirements until one early morning or late night when they dont work. Have you written any specs against an external authorization service, payment processor, or other web service? How could you speed things up with VCR and Fakeweb? 11. Toward test-driven development Whew. Weve come a long way with our address book application. At the beginning of the book it had the functionality we were after, but zero tests. Now its reasonably tested, and weve got the skills necessary to go in and plug any remaining holes. But have we been doing test-driven development? Strictly speaking, no. The code existed long before we added a single spec. What weve been doing is closer to exploratory testingusing tests to better understand the application. To legitimately practice TDD, well need to reverse thattests come first, then the code to make those tests pass, then some refactoring. Here are some steps to help you get there from here. Practice testing the small things Diving into TDD through complex new features is probably not the best way to get comfortable with the process. Focus instead on some of the lower-hanging fruit in your application. Bug fixes, basic instance methods, controller-level specsthese are typically straightforward tests, usually requiring a little bit of setup and a single expectation. Just remember to write the spec before tackling the code! Be aware of what youre doing As youre working, think about the processes youre using. Take notes. Have you written a spec for what youre about to do? Does the spec cover edge cases and fail states? Keep a mental checklist handy as you work, making sure youre covering what needs to be covered as you go. Short spikes are OK Test-driven development doesnt mean you can only write code once its got a test to back it. It means you should only write production code after youve got the specs. Spikes are perfectly fine! Depending on the scope of a feature, Ill often spin up a new Rails application to tinker with an idea. Ill typically do this when Im experimenting with a library or some relatively wholesale change. For example, I recently worked on a data mining application in which I needed to completely overhaul the applications model layer, without adversely affecting the end user interface. I knew what my basic model structure would look like, but I needed to tinker with some of the finer points to fully understand the problem. By spiking this in a standalone application, I was free to hack and experiment within the scope of the actual problem Im trying to solvethen, once Id determined that 117 11. Toward test-driven development 118 I understood the problem and have a good solution for it, I opened up my production application, wrote specs based on what I learned in my tests, then wrote code to make those specs pass. For smaller-scale problems Ill work in a fork of the production application, doing the same type of experimentation and keeping an eye on which files are getting changed. Going back to my data mining project, I recently had a feature to add involving how users would view already-harvested data. Since I already had the data in my system, I created a branch within my version control system and spiked a simple solution to make sure I understood the problem. Once I had my solution, I commented out what Id written, wrote my specs, and then reapplied my work. As a general rule, I try to retype my work as opposed to just uncommenting it (or copying and pasting); I often find ways to refactor or otherwise improve what I did the first time. Write a little, test a little is also OK If youre still struggling with writing specs first, it is acceptable to code, then test; code, then testas long as the two are closely coupled. Id argue, though, that this approach requires more discipline than just writing tests first (after untested spikes). In other words, while I say its OK, I dont think its ideal. But if it helps you get used to testing then go for it. Strive to write request specs first Once you get comfortable with the basic process and the different levels at which to test your application, its time to turn everything upside down: Instead of building model specs and then working up to controller and request specs, youll start with request specs, thinking through the steps an end user will follow to accomplish a given task in your application. This is essentially whats referred to as outside-in testing. As you work to make the request spec pass, youll recognize facets that are better-tested at other levelsfor example, validations at the model level; authorization nuances at the controller level. A good request spec can serve as an outline for all of the tests pertaining to a given feature, so learning to begin by writing them is a valuable skill to have. Keep practicing Finally, it might go without saying, but youll get better at the process with lots of practice. Again, I find throwaway Rails applications to be great for this purposecreate a new app (say, a blogging app or to-do list), and practice TDD as you build a feature set. What determines your features? Whatever testing skill youre building. Want to get better at specs for email? Make that to-do list send a projects tasks to its owner with the click of a button. Dont wait for a feature request to arise in a production project. 12. Parting advice Youve done it! If youve been adding tests to your application as you worked through the patterns and techniques outlined in this book, you should have the beginnings of a well-tested Rails application. Im glad youve stuck with it this far, and hope that by now youre not only comfortable with tests, but maybe even beginning to think like a true test-driven developer and using your specs to influence your applications actual under-the-hood design. And dare I say, you might even find this process fun! To wrap things up, here are a few things to keep in mind as you continue down this path: Make time for testing Yes, tests are extra code for you to maintain; that extra care takes time. Plan accordingly, as a feature you may have been able to complete in an hour or two before might take a whole day now. This especially applies when youre getting started with testing. However, in the long run youll recover that time by working from a more trustworthy code base. Keep it simple If you dont get some aspects of testing right awayin particular, request specsdont worry about it. They require some additional setup and thinking to not just work, but actually test what you need to test. Dont stop testing the simpler parts of your app, thoughbuilding skills at that level will help you grasp more complicated specs sooner rather than later. Dont revert to old habits! Its easy to get stuck on a failing test that shouldnt be failing. If you cant get a test to pass, make a note to come back to itand then come back to it. The resources in the next chapter will help you get past those sticking points and on to becoming a better tester and Rails developer. Use your tests to make your code better Dont neglect the Refactor stage of Red-Green-Refactor. Learn to listen to your teststheyll let you know when something smells bad in your code, and help you clean house without breaking important features. 119 12. Parting advice 120 Sell others on the benefits of automated testing I still know far too many developers who dont think they have time to write test suites for their applications. (I even know a few who think that being the only person in the world who understands how a brittle, spaghetti-coded application works is actually a form of job securitybut I know youre smarter than that.) Or maybe your boss doesnt understand why its going to take a little longer to get that next feature out the door. Take a little time to educate these people. Tell them that tests arent just for development; theyre for your applications long-term stability and everyones long-term sanity. Show them how the tests workIve found that showing off a request spec with JavaScript dependencies, as we put together in chapter 8, provides a wow factor to help these people understand how the extra time involved in writing these specs is time well-spent. Goodbye, for now Youve now got all the tools you need to do basic automated testing in your Rails projects, using RSpec, Factory Girl, Capybara, and DatabaseCleaner to help. These are the core tools I use daily as a Rails developer, and the techniques Ive presented here show how I learned to effectively use them to increase my trust in my code. I hope Ive been able to help you get started with these tools as well. Thats the end of Everyday Rails Testing with RSpec, but I hope youll keep me posted as you work toward becoming a test-driven developer. If you have any comments, insights, suggestions, revelations, complaints, or corrections to make to the book, feel free to send them my way: Email: [email protected] Twitter: @everydayrails Facebook: http://facebook.com/everydayrails GitHub: https://github.com/ruralocity/everyday_rails_rspec_rails32/issues I also hope youll follow along with new posts at Everyday Rails (http://everydayrails.com/). Thanks again for reading, Aaron https://twitter.com/everydayrails http://facebook.com/everydayrails https://github.com/ruralocity/everyday_rails_rspec_rails_3_2/issues More testing resources for Rails While not exhaustive, the resources listed below have all been reviewed by yours truly and can each play a role in giving you a better overall understanding of Rails application testing. RSpec RSpec.info RSpec.info is the official site for RSpec and provides a couple of basic examples and, more importantly, links to RDocs for all of RSpecs components. You can learn a lot about good use of RSpec through this documentation; take the time to read through it. http://rspec.info RSpec documentation on Relish Another excellent source for RSpec documentation and examples is the Cucumber-generated output available at Relish. This also includes past documentation going back to RSpec 2.0, should you need it. https://www.relishapp.com/rspec Better Specs Better Specs is a really nice collection of illustrated best practices to employ in your test suite. http://betterspecs.org Peepcode Great news! The folks at Peepcode are revisiting RSpec, replacing their early series of now-outdated videos with up-to-date versions. To date two new videos have been released. If youd like to see someone in the act of test-driving development, be sure to check them out. http://peepcode.com The RSpec Book: Behaviour-Driven Development with RSpec, Cucumber, and Friends This book, written by RSpecs lead developer, David Chelimsky, provides a thorough look at the entire RSpec ecosystem. I strongly recommend reading this book after youve got the basics down or if youre interested in using RSpec outside of Rails. http://pragprog.com/book/achbd/the-rspec- book 121 More testing resources for Rails 122 Railscasts I dont knowa single Rails developer whos not familiar with Ryan Bates top-notch screencast series, Railscasts. Ryan has done a number of episodes on testing; many either focus on RSpec or include it as part of a larger exercise. Be sure to watch the episode How I Test, which in part inspired this book. http://railscasts.com/?tag_id=7 Code School Code Schools Testing with RSpec is a video/hands-on tutorial combination. The course includes content and activities covering configuration, hooks and tags, mocks and stubs, and custom matchers. For a look at Rails default testing framework, check out Rails Testing for Zombies, too. http://www.codeschool.com/courses/ The RSpec Google Group The RSpec Google Group is a fairly active mix of release announcements, guidance, and general support for RSpec. This is your best place to go with RSpec-specific questions when you cant find answers on your own. http://groups.google.com/group/rspec Rails testing Rails Test Prescriptions: Keeping Your Application Healthy This book by Noel Rappin is my favorite book on Rails testing. Noel does a fine job covering a wide swath of the Rails testing landscape, from Test::Unit to RSpec to Cucumber to client-side JavaScript testing, as well as components and concepts to bring everything together into a cohesive, robust test suite. http://pragprog.com/book/nrtest/rails-test-prescriptions Another good resource from Noel is his talk from Rubyconf 2012, titled Testing Should Be Fun. Watch it when you notice your test suite running slow or getting difficult to manageor better yet, before youve reached that point so you know how to avoid it in the first place. Video available at http://confreaks.com/videos/1306-rubyconf2012-testing-should-be-fun; slides at https://speakerdeck.com/noelrap/testing-should-be-fun Rails Tutorial The book I wish had been around when I was learning Rails, Michael Hartls Rails Tutorial, does the best job of any Rails introduction Ive seen of presenting Rails in the way youll be developing in itthat is, in a test-driven fashion. Also available as a series of screencasts, if thats your learning preference. http://ruby.railstutorial.org More testing resources for Rails 123 Agile Web Development with Rails Agile Web Development with Rails by Sam Ruby (with Dave Thomas and David Heinemeier- Hansson) is the book that was available when I got started with Rails. Back in its first edition I thought testing was treated like an afterthought; however, the current, fourth edition does a much better job of weaving tests into the development process. http://pragprog.com/book/rails4/agile-web- development-with-rails About Everyday Rails Everyday Rails is a blog about using the Ruby on Rails web application framework to get stuff done as a web developer. Its about finding the best plugins, gems, and practices to get the most from Rails and help you get your apps to production. Everyday Rails can be found at http://everydayrails.com/ 124 About the author Aaron Sumner is a Ruby developer in the heart of Django country. Hes developed web applications since the mid-1990s. In that time hes gone from developing CGI with AppleScript (seriously) to Perl to PHP to Ruby and Rails. For the most part, his work has been relegated to the education research and instructional technology sectors, which is why youve probably never heard of him until now. When off the clock and away from the text editor, Aaron enjoys photography, baseball (go Cards), college basketball (Rock Chalk Jayhawk), and bowling. He lives with his wife, Elise, along with four cats and a dog in rural Kansas. Aarons personal blog is at http://www.aaronsumner.com/. Everyday Rails Testing with RSpec is his first book. 125 Colophon The cover image of a practical, reliable, red pickup truck is by iStockphoto contributor Habman_- 18. I spent a lot of time reviewing photos for the covertoo much time, probablybut picked this one because it represents my approach to Rails testingnot flashy, and maybe not always the fastest way to get there, but solid and dependable. And its red, like Ruby. Maybe it should have been green, like a passing spec? Hmm. http://www.istockphoto.com/stock-photo-16071171-old-truck-in-early-morning-light.php?st=1e7555f http://www.istockphoto.com/user_view.php?id=4151137 126 Change log April 15, 2013 Moved sample code and discussion to GitHub; see chapter 1. Updated chapters 9 and 10. Reworked the JavaScript/Selenium example in chapter 9. March 9, 2013 Fixed stray references to should in multiple places. Fixed errant model spec for phones in chapter 3. Added the changelog to the end of the book. February 20, 2013 Fixed formatting error in user feature spec, chapter 8. Correctly test for the required lastname on a contact, chapter 3. Fixed minor typos. February 13, 2013 Replaced use of should with the now-preferred expect() syntax throughout most of the book (chapters 9 and 10 excepted; see below). Covered the new Capybara 2.0 DSL; chapter 8 now covers feature specs instead of request specs. Reworked initial specs from chapter 3 to skip factories and focus on already available methods. Chapter 4 is now dedicated to factories. Copy edits throughout. December 11, 2012 Added new resources to the resources section. Added warnings about the overuse of of Factory Girls ability to create association data to chapter 4. 127 Change log 128 November 29, 2012 Reformatted code samples using Leanpubs improved highlighting tools. Added mention of changes in Capybara 2.0 (chapter 8). Added warning about using Timecop.return to reset the time in specs (chapter 10). August 3, 2012 Added the change log back to the book. Replaced usage of == to eq throughout the book to mirror best practice in RSpec expectations. Added clarification that you need to re-clone your development database to test every time you make a database change (chapter 3). Added a note on the great factory debate of 2012 (chapter 3). Added a section about the new RSpec expect() syntax (chapter 3). Fixed incomplete specs for the #edit method (chapter 5). Added an example of testing a non-CRUD method in a controller (chapter 5). Added tips on testing non-HTML output (chapter 5). Fixed a typo in the :message factory (chapter 5). Fixed typo in spelling of transactions (chapter 8). Added a simple technique for testing Rake tasks (chapter 10). July 3, 2012 Corrected code for sample factory in chapter 5. June 1, 2012 Updated copy throughout the book. Added Testing the Rest chapter (chapter 10), covering email specs, time-sensitive function- ality, testing HTTP services, and file uploads. May 25, 2012 Revised chapter 8 on request specs. Added chapter 9, covering ways to speed up the testing process and tests themselves. Added chapter 11, with tips for becoming a test-driven developer. Corrected typos as indicated by readers. Change log 129 May 18, 2012 Added chapter 4, which expands coverage on Factory Girl. Refactored controller testing into 3 chapters (basic, advanced, cleanup). Advanced includes testing for authentication and authorization. Added acknowledgements and colophon. Moved resources chapter to an appendix. Corrected typos as indicated by readers. May 11, 2012 Added sample application code for chapters 1,2, and 3. Revised introduction chapter with more information about source code download and purpose. Revised setup chapter with changes to generator configuration and Factory Girl system requirements, and other minor changes. Revised models chapter to follow along with the sample code better, explain some uses of Factory Girl, and move Faker usage out of chapter (to be added back in chapter 4). Switched to using bundle exec when calling rake, rspec, etc. from the command line. Added specific gem versions in Gemfile examples. Corrected typos as indicated by readers. May 7, 2012 Initial release.
21 CFR Part 11 Compliance Manual for Endosafe® ESV V5.5 and Higher, Endosafe® nexgen-PTST V10.1 and Higher, and CR CortexT V1.1 and Higher (PICRFCOMPLI001-01)