The RSpec Book Review
Due to the popularity of Ruby on Rails and the fact that Test-Driven Development (TDD) is baked into the framework, Ruby testing is rapidly evolving into something much more agile. The RSpec Book explores one of the more popular testing frameworks, as well as Cucumber, another Behaviour-Driven Development (BDD) tool. Does the book help readers quickly moves these concepts into practice? Read on to find out.It took me a while to pick up on TDD with my Rails development experiences and once I did, I quickly felt constrained by the way it boxed me into its flow. I also wanted something I could comfortably apply to my Ruby code outside of Rails. Fortunately, I was not the only developer feeling this way which might explain the rising popularity of RSpec. The RSpec Book is a five part title that illustrates Ruby and Rails BDD practices using RSpec, Cucumber and related tools. Part 1 provides and introduction to RSpec and Cucumber by showing the relationships between them as well as the business cases and technology drivers for using them. There are also several examples of RSpec and Cucumber code in action to show how the syntax works and the output the type of output such tests generate. This part also walks readers through a code-breaker game app scenario and how to think in terms of RSpec and Cucumber vocabularies that will be used to define and refactor the tests. Part 2 is the briefest of the five, covering the principles and reasons to buy into Behaviour-Driven Development. Part 3 contains over 100 pages discussing RSpec with extensive of definitions of RSpec's vocabulary accompanied by code listings of these keywords in action along with helper methods, shared and nested example groups. Chapters dedicated to the RSpec::Expectations, RSpec::Mock frameworks and how their syntax of describe, expect, should/should_not, mock and stub offer greater power and flexibility to generated tests. The authors also warn readers of common pitfalls (brittle examples, missing coverage, over-speculation) and how to avoid them. The 'Friends' portion of the titles comes into play with the sections on other tester tools such as Autotest and the RCov code coverage tool. Part 3 concludes with a chapter on extending RSpec's functionality. Part 4 explores Cucumber in much the same way, and introduces Gherkin (what is with these goofy names?), the Given/When/Then keywords, tags and a healthy number of examples. Part 5, the largest portion of the book, shows how RSpec and Cucumber work in the Rails world, a place where they will likely be used the most by developers. The Webrat gem is given its own chapter and shows why it is such an integral part of testing web apps with RSpec via its simulated browser-like behavior. There's also a Webrat keyword review for working with forms (fill_in, check/uncheck, choose, select and the like. Chapters on getting Webrat to work with Selenium and writing Rails Model, View and Controller specs. Appendix A introduces RubySpec, a project that attempts to "write a complete, executable specification for the Ruby programming language", followed by an appendix summarizing RSpec's built-in expectations.
I'm usually not a big fan of books written by multiple authors since the voice and cadence context switching can sometimes be disorienting and/or the content can be redundant in places if the book is not properly edited. While its easy to identify when the RSpec authors take turns contributing to the book's content, the impact fortunately isn't too distracting. Still, there is sometimes too clean of a break between the RSpec and Cucumber discussions, giving me the unintended impression of a lack of continuity between the two technologies. And even after reading the book, I'm still having a hard time taking up BDD Cucumber (what the authors like to call "the outer circle" of testing) practices. I accept the practical argument of writing tests in the language of the customer, but the overhead of learning the dialect on top of learning RSpec is frankly a lot of effort that I don't have time to invest in most of my projects that need super-fast turnarounds (and if it's a web app with a crazy fast deadline, I usually just call upon Django instead). I can see myself considering Cucumber for big projects with a great deal of customer stories and ample amount of time dedicated to the BDD definitions and coding. But in the end, my customers don't care about the wizbang tools and English sentence-like descriptions; they just want a rock-stable application that never breaks.
Even though I have yet to be smitten by the light that shines from Cucumber, I am completely sold on RSpec - it's the first gem (along with Webrat and Autotest) I add to a new Rails project. From that perspective, the RSpec book is one that finally helps me understand the vocabulary and the variety of scenarios that RSpec can help test.
If you're already an RSpec master, this book may not have much new to offer. But if you're new to Ruby and Rails development and have started feeling constrained by the TDD default tools in Rails, RSpec will make testing, dare I say, fun - and The RSpec Book will get you, dare I say, excited about testing your code.
Title: The RSpec Book: Behaviour-Driven Development with RSpec, Cucumber, and Friends Authors: David Chelimsky, Dave Astels, Zach Dennis, Aslak Hellesoy, Bryan Helmkamp and Dan North Publisher: Pragmatic Bookshelf ISBN: 978-1-93435-637-1 Pages: 448 Price: $38.99 (paper), $24.00 (eBook)