Project

two-step

0.0
No commit activity in last 3 years
No release in over 3 years
I don't like Cucumber. I don't need plain text stories. My clients either read code or don't read any test documents, so Cucumber is mostly useless to me. But often, especially in full integration tests, it would be nice to have steps in a test.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0.4.0
~> 4.11.1

Runtime

< 3, >= 2.6
 Project Readme

Two Step

( or: why would I want to relearn how to write specs? )

Two-Step allows you to chain examples into a series of steps that run in sequence and which stop when a step fails. It's often incredibly useful to be able to aseemble a series of tests that should all pass, but where completely isolating them is less than sensible.

( Two Step is the RSpec 2 compatible version of RSpec Steps - which can be found here: https://github.com/LRDesign/rspec-steps )

One excellent example is web site integration tests. With RSpec steps you can do:

     steps "Login and change password" do
       it "should show the login form" do
         visit root
         page.should have_text "Login"
       end

       it "should successfully log in" do
         fill_in :name, "Johnny User"
         click "Login"
         page.should have_text "Welcome, Johnny!"
       end

       it "should load the password change form" do
         click "My Settings"
         click "Update Password"
         page.should have_selector("form#update_password")
       end

       it "should change the user's password successfully" do
         fill_in :password, "foobar"
         fill_in :password_confirmation, "foobar"
         click "Change Password"
         page.should have_text "Password changed successfully!"
         User.find_by_name("Johnny User").valid_password?("foobar").should be_true
       end

     end

The examples above will be run in order. State is preserved between examples inside a "steps" block: any DB transactions will not roll back until the entire sequence has been complete.

If any example inside the "steps" block fails, all remaining steps will be marked pending and therefore skipped.

Rationale

RSpec's philosophy is that all examples should be completely independent. This is a great philosophy for most purposes, and we recommend you stick to it in almost all cases. BUT, that complete separation of examples really sucks when you're trying to write long stories involving many requests. You are usually stuck with three choices:

  1. Write a sequence of examples, each of which repeats the behavior of all previous examples. Downside: horrendously inefficient.
  2. Write a single huge example which performs the entire story. Downside: only one description, no independent reporting of the steps of the story.
  3. Use Cucumber. Downside: We agree totally with this guy: http://bit.ly/dmXqnY

Two-Step intentionally breaks RSpec's "independent" philosophy to let us get the only thing we really want from Cucumber - the ability to execute some examples in sequence, and skip subsequent steps after a failure.

Caveats and cautions

Don't call "describe" inside of "steps". The behavior is undefined and probably bad. It's hard to imagine what this should actually mean in any case. Future versions of rspec-steps will consider this an error.

Any call to "before" inside a steps block is treated like before(:all) and is run only once before the first step. Or perhaps more accurately, any call to before() is treated like before(:each) ... but for these purposes the entire steps block is treated like a single example.

Advanced stuff: shared steps

If you have (for example) two user stories that share the same first N steps but then diverge, you can DRY your code out with shared_steps blocks, like so:

   shared_steps "For a logged-in user" do
     it "should have login form"
       visit root
       page.should have_selector "form#login"
     end

     it "should log the user in" do
       fill_in :name, "Johnny User"
       page.should have_text "Welcome, Johnny!"
     end
   end

   steps "updating password" do
     perform_steps "For a logged-in user"

     it "should update the password" do
       ...
     end
   end

   steps "uploading a profile picture" do
     perform_steps "For a logged-in user"

     it "should upload a picture" do
        ...
     end
   end

Versions and Dependencies

The goal (sadly unfulfilled) is to try to be compatible with as many versions of RSpec 2.x as possible. Frankly, Two-Step is more than a little bit of a hack, and intrudes wholesale on RSpec's private interfaces. We make good use of Travis to check compatibility, however. You can check what versions of RSpec and Ruby Two-Step works with here: https://travis-ci.org/LRDesign/two-step