Wednesday, August 26, 2009

Learning RSpec/Cucumber - Day 1

I've wanted to improve my testing for some time now and so I'm pairing on the RSpec book with my friend, Allan Miller. We're going to work through the book/examples in tandem and get together regularly to review what we've learned. We just downloaded the RSpec book by Pragmatic and assigned Chapters 1-3 (this is the book that was highly recommended by our Ruby Meetup folks). We'll work through this and then meet in a couple of days to see how things went and move on to the next group of chapters.

For now, I'm running all this on my MacBook, without any IDE... just straight terminal access and MacVIM. Chapters 1 & 2 are introduction; chapter 3 is where you start actually doing stuff.

First problem (minor) that I ran into was that the rspec installation wasn't clean; I got the following errors:

Successfully installed rspec-1.2.8

1 gem installed

Installing ri documentation for rspec-1.2.8...

Installing RDoc documentation for rspec-1.2.8...

Could not find main page README.rdoc

Could not find main page README.rdoc

Could not find main page README.rdoc

Could not find main page README.rdoc

So far, this doesn't seem to have affected anything -- I just ignored it and moved on (so far, I don't use the ri or RDoc information at all).

The cucumber installation was a little more disconcerting:

Installing ri documentation for builder-2.1.2...

ERROR: While generating documentation for builder-2.1.2

... MESSAGE: Unhandled special: Special: type=17, text=""

... RDOC args: --ri --op /Library/Ruby/Gems/1.8/doc/builder-2.1.2/ri --title Builder -- Easy XML Building --main README --line-numbers --quiet lib CHANGES Rakefile README doc/releases/builder-1.2.4.rdoc doc/releases/builder-2.0.0.rdoc doc/releases/builder-2.1.1.rdoc --title builder-2.1.2 Documentation

(continuing with the rest of the installation)

but again, it was only documentation so I moved on from here as well (have to pick your battles, you know).

Everything went just fine, except for a couple of glitches: one was just some missing default snippets and the other was a result of those missing snippets. Here's what happened:
  1. The first attempt at cucumber did NOT display the snippets for the 3 undefined steps (pp 27-28 in my beta copy of the book) as expected. Rather than being able to copy and modify them I had to type them in by hand. [I tried the command twice just to make sure.. sure enough, didn’t display, although a later point where I had only one step undefined, it DID display the snippet template. Go figure.]
  2. Because of #1, I made a transcription error in the regexp for step 3: I omitted the ‘^’ exclusion character. This cost me several minutes (regexp’s are like that!). I have a feeling that there may be more of these... hopefully I’ll be able to get the step snippets more reliably in the future and then just copy them into my step definitions as a starting point.
All in all, a successful beginning... But I do have a concern -- given that we're using regexp's, there are MANY opportunities for small errors that can cost time. Seems like a bit of a contradiction: cucumber is a "higher level" testing language than rspec, but it uses one of the lowest level syntaxes to accomplish this.

I'll just have to wait and see what this leads to... and I'm sure going to copy/paste those snippets whenever I can.


  1. I had the same problem, no snippets! I googled and...found your page :) Did you work out what the problem was? Otherwise I'm enjoying this book a lot...

  2. @heptat...
    Nope; haven't figured it out yet. It's happened again (and you can see it in section 4.1 of the book as well). When I get a chance, I'm going to see if there's anything posted about this on the Cucumber site.

    And yes, good book so far. I'm looking forward to the next beta as I've found several references to "not yet" sections of the book.

  3. @heptat...
    I figured out the problem -- at least for why we get NO snippet text when there are no step definitions. Cucumber supports both ruby and python programming languages and has to figure out what language you're using before it can actually program the snippets. The way they figure out the language is they look at the step file but, of course, there's no step file so it can't figure out the language. Therefore, the whole snippet_text definition fails and you end up with a blank entry for each undefined step definition. Haven't figured out how to fix this yet...

  4. @heptat...
    I've submitted a patch to cucumber to fix the problem; I am hoping that it will be included in the master before too long. Until then, I created a fork (based on cucumber 0.3.98) at git:// in case you're interested.

  5. It looks like cucumber just looks for any file under features with a familiar extension.
    I did "touch features/fake.rb" and got snippets on the next run.

  6. @cybersquid..
    You're absolutely right... and that's a quick fix for the problem.

  7. Thank you, cybersquid.

    That's wright!!