Skip to content

RSpec, Cucumber, and BDD Testing

I got real sick while on vacation in the mountains (a combination of cold/flu and altitude sickness) and had some down-time to work on my rails app.  My main goal is to get all of the user login/user invite functionality locked down before I start expanding the feature set of the app itself, as well as to get the existing code (not a whole lot) under test.

The QuickBase gem I’ve been working on uses RSpec for the unit tests, and I’d heard good things elsewhere so I bought the PDF version of The RSpec Book before we headed to the airport.  I’ve done TDD before, and am used to the xUnit syntax and philosophy but it was real interesting delving into Behavior Driven Development (BDD) and the way they used Ruby to reduce testing to real english sentences.  Below is an actual working example of my login integration tests using Cucumber:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Feature: Login to application

        So that I can access my application's info
        As a user
        I want to login to the application
       
        Scenario: Access to login page from home page
                Given I am on the homepage
                When I follow "Login"
                Then I should be on the login page
               
        Scenario: A valid user can login
                Given I am on the login page
                When I login as "mytest" with password "mytest"
                Then I should see "You're now logged in"
                And I should be on the home page

I gotta say, to actually run the tests and see all green after writing that made me grin ear-to-ear.  I should note that it took me awhile (and a few swears) to get the constituent parts installed on my Macbook Pro (nokogiri in particular has a bug involved with installing it to vendor/gems and is very opinionated about your installed libxml2 version).  After a little bit of configuration heartache, though, I got things running really well.

The other slight issue I had in getting RSpec working with my model unit tests was that RSpec adds some generators for your use in Rails (rspec_model, for instance, which wraps the built-in ‘script/generate model’) except that it generates the appropriate test code under /spec instead of /test.  I had obviously created many models, controllers, etc. without using the wrapped generators, so I had some work to do to manually establish the /spec folder hierarchies.  I also had to google a bit to figure out how to move over and include my fixtures from /test.  Ultimately, though, things settled down pretty quickly and I’m loving it.

I guess I should also declare that I_KNOW_I_AM_USING_AN_OLD_AND_BUGGY_VERSION_OF_LIBXML2 and loving it.

Also: I got Google Wave developer sandbox access.  More to come wheeeeeeeeeeee!

{ 5 } Comments

  1. Dan Sharp | August 18, 2009 at 4:30 pm | Permalink

    Hi Zachery!

    I’m getting the I_KNOW_I_AM_USING_AN_OLD_AND_BUGGY_VERSION_OF_LIBXML2 warning when I run cucumber. I can’t seem to find any online help other than updating libxml, but I don’t want/need to do that. Did you do that or did you define that constant? And if the latter… where did you define the constant? I can’t seem to figure it out. *sigh*

    -Dan

  2. Dan Sharp | August 18, 2009 at 4:35 pm | Permalink

    Hi again…

    My bad… I didn’t read the warning closely enough. I opened up env.rb under /features/support/ and defined that at the very top and the warnings are gone now. (I had foolishly defined it at the bottom of the env.rb. Doh!)

    Anyway, as one who is also just jumping into the rspec/cucumber world, I’m excited to see how this will affect my development process… hopefully for the better!

    -Dan

  3. whazzmaster | August 18, 2009 at 5:12 pm | Permalink

    Hi Dan-

    I actually did end up upgrading libxml2 using MacPorts. I really, REALLY didn’t want to go down that path but in the end I did it. I should note, however, that even after I updated via the port command I had to source all my .profiles (actually, I think I ended up restarting the terminal to get it to recognize the right paths) before the dreaded LONGEST_SCOLDING_CONSTANT_NAME_IN_HISTORY went away.

  4. whazzmaster | August 18, 2009 at 5:28 pm | Permalink

    Oops, I should also note that once you install MacPorts, you have to re-install EVERYTHING to get it to use the new libxml2. Ruby, RubyGems, all your gems, everything. That part kinda sucks, just to get rid of the warning; it may be better to just define the damn constant unless you’re actively encountering the terrifying libxml2 bugs that nokogiri warned you about.

  5. manish | October 5, 2009 at 6:49 am | Permalink

    Hi. Can you please invite me on google wave with email provided if you don’t mind? I just can’t resist to feel it !!! Thank you.

Post a Comment

Your email is never published nor shared. Required fields are marked *