Updated RSpec Cheatsheet

By March 15, 2013Technical

If you’re a regular reader you’ll know that we’ve been writing a lot of API code recently for internal development. This is a team effort with lots of coding happening in parallel in separate modules, so making sure they all work together correctly is critical.

The way to do this is with comprehensive unit-testing at the lower levels, and conformance testing between APIs themselves. We’ve been coding in Ruby, so RSpec was a natural choice. RSpec is nice because it’s practically provided by default (“batteries included!”), really easy to pick up and most importantly, really easy to read. The importance of readability can’t be overstated when you’re working in a team where different people are writing, implementing and validating specifications.

Readability is good, but you have to be able to write RSpec as well. Not all of us are dyed-in-the-wool developers, so a cheatsheet for common RSpec syntax is really handy. Luckily for us one of RSpec’s developers, Dave Astel, made just such a thing! We’ve been using it and it’s been great for getting up to speed very quickly.

Now, RSpec’s syntax has come a long way since Dave made his cheatsheet in 2006 and we think it could use a bit of a sprucing up. So we did just that! We’ve updated the cheatsheet to use the new expect syntax that deprecates should, and generally makes things a bit more consistent.

The Updated RSpec Cheatsheet

We recommend printing it out (it’s double-sided) and laminating it, and keeping it on your desk for those times that you need it.

The syntax for mock objects is expected to be updated further to also use expect, but it’s not finalised yet, so we’ll still be using should for a little while more. We won’t be too surprised if that arrives for RSpec 3.

The should-based syntax will be deprecated and removed sooner or later, so you might as well start using expect right away for anything new. We hope you find the updated cheatsheet useful, and as always we’d be more than happy to hear what you think about it!


Leave a Reply