Introducing Spectie, a behavior-driven-development library for RSpec 12

Posted by ryan Mon, 02 Nov 2009 03:34:00 GMT

I'm a firm believer in the importance of top-down and behavior-driven development. I often start writing an integration test as the first step to implementing a story. When I started doing Rails development, the expressiveness of Ruby encouraged me to start building a DSL to easily express the way I most-often wrote integration tests. In the pre-RSpec days, this was just a subclass of ActionController::IntegrationTest that encapsulated the session management code to simplify authoring tests from the perspective of a single user. As the behavior-driven development idea started taking hold, I adapted the DSL to more-closely match those concepts, and finally integrated it with RSpec. The result of this effort was Spectie (rhymes with necktie).

The primary goal of Spectie is to provide a simple, straight-forward way for developers to write BDD-style integration tests for their projects in a way that is most natural to them, using existing practices and idioms of the Ruby language.

Here is a simple example of the Spectie syntax in a Rails integration test:

Feature "Compelling Feature" do
  Scenario "As a user, I would like to use a compelling feature" do
    Given :i_have_an_account, :email => "ryan@kinderman.net"
    And   :i_have_logged_in

    When  :i_access_a_compelling_feature

    Then  :i_am_presented_with_stunning_results
  end

  def i_have_an_account(options)
    @user = create_user(options[:email])
  end

  def i_have_logged_in
    log_in_as @user
  end

  def i_access_a_compelling_feature
    get compelling_feature_path
    response.should be_success
  end 

  def i_am_presented_with_stunning_results
    response.should have_text("Simply stunning!")
  end
end

Install

Spectie is available on GitHub, Gemcutter, and RubyForge. The following should get it installed quickly for most people:

% sudo gem install spectie

For more information on using Spectie, visit http://github.com/ryankinderman/spectie.

Why not Cucumber or Coulda?

At the time that this is being written, Cucumber is the new hotness in BDD integration testing. My reasons for sticking with Spectie instead of switching to Cucumber like the rest of the world are as follows:

  • Using regular expressions in place of normal Ruby method names seems like a potential maintenance nightmare, above and beyond the usual potential.
  • The layer of indirection that is created in order to write tests in plain text doesn't seem worth the cost of maintenance in most cases.
  • Separating a feature from its "step definitions" seems mostly unnecessary. I like keeping my scenarios and steps in one file until the feature becomes sufficiently big that it warrants extra organizational consideration.

These reasons are more-or-less the same as those given by Evan Light, who recently published Coulda, which is his solution for avoiding the cuke. What sets Spectie apart from Coulda is its reliance on and integration with RSpec. The Spectie 'Feature' statement has the same behavior as an RSpec 'describe' statement, and the 'Scenario' statement is the same as the RSpec 'example' and 'it' statements. By building on RSpec, Spectie can take advantage of the contextual nesting provided by RSpec, and rely on RSpec to provide the BDD-style syntax within what I've been calling a scenario statement (the words after the Given/When/Thens). Coulda is built directly on Test::Unit. I'm a firm believer in code reuse, and RSpec is the de facto standard for writing BDD-style tests. Spectie, then, is a feature-driven skin on top of RSpec for writing BDD-style integration tests. To me, it only makes sense to do things that way; as RSpec evolves, so will Spectie.

  1. face blog {{count}} months later:

    Thanks a lot for this post...

  2. HID KIT {{count}} months later:

    Developing library is the good margin around the boundary.

  3. Waterproof Jacket {{count}} months later:

    Nicely presented information in this post, I prefer to read this kind of stuff. The quality of content is fine and the conclusion is good. Thanks for the post.

  4. jeans clearance over 1 year later:

    You really make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand. It seems too complicated and very broad for me. I am looking forward for your next post.http://jeansfreeshipping.com/

  5. Bonbons over 1 year later:

    I didnt' understand much of your article, as it's quite complicated...

  6. Bonbons over 1 year later:

    I'll definitely forward this to my wife!

  7. Round Rock Painting Companies almost {{count}} years later:

    I really enjoy your site .. I've shared a lot about your facebook account.

  8. Samsung Admire almost {{count}} years later:

    When I started doing Rails development, the expressiveness of Ruby encouraged me to start building a DSL to easily express the way I most-often wrote integration tests

  9. Hivedb over {{count}} years later:

    Really good example of the Spectie syntax in the Rails integration test.

  10. Quiz for kids about {{count}} years later:

    Quiz for kids

    Nice blog on education strategy, Thanks for such a good infomation on education.

  11. master-essay.com about {{count}} years later:

    I must say I enjoy how I existing your notions. Outstanding post. Extremely educational. Many thanks.

  12. http://www.tigerauonlineasics.com/ over {{count}} years later: