[rspec-users] Reusing story snippets

Jim Morris wolfmanjm at gmail.com
Mon Jun 16 19:18:29 EDT 2008


Along similar lines is there a way to do the equivalent of
before(:all) and after(:all) or after(:each) in stories?

Basically I have a similar situation as above, but I need to make sure
the user is logged out after each scenario. or that the user is logged
in once at the start of all scenarios then logged out after them
regardless of errors etc.

Thanks


On Jun 14, 7:40 am, David Chelimsky <dchelim... at gmail.com> wrote:
> On Jun 14, 2008, at 5:03 AM, Mikel Lindsaar wrote:
>
> > I find myself doing this:
>
> > Scenario "logged in user visiting the home page" do
> >  Given "A logged in user" do
> >    a_logged_in_user
> >  end
>
> >  When "..."
> >  Then "..."
> > end
>
> Things have evolved a bit since Story Runner first came out. The
> approach you are using here is what we call in-line steps, and was the
> only option back then.
>
> You can now use disconnected steps in both plain text and Ruby:
>
> require 'steps/visitors'
>
> Story "logged in users see more stuff", %(
>    As a registered user
>    I want to see more stuff than non-registered users
>    So I can feel like I'm getting some benefit in return for giving
>      up my personal information
> ), :steps => :visitors do
>    Scenario "logged in user visits home page" do
>      Given "I am logged in as David"
>      When "I visit the home page"
>      Then "I should see the message 'Welcome David'"
>    end
> end
>
> # in steps/visitors
>
> steps_for :visitors do
>    Given "I am logged in as $name" do |name|
>      # create a user w/ name and log in as that user
>    end
>
>    When ".."
>    Then ".."
> end
>
> This approach really cleans up the story code leaving the informative
> bits while hiding the redundant detail.
>
> HTH,
> David
>
> > The a_logged_in_user method is a helper method in helper.rb which sets
> > up the state so that the user can browse the website.
>
> > Later in the story of course, I can just do 'Given "A logged in user"
> > and it will get the previous definition.
>
> > Is there any way to avoid that duplicated Given call at the top of
> > almost every story?
>
> > Mikel
> > _______________________________________________
> > rspec-users mailing list
> > rspec-us... at rubyforge.org
> >http://rubyforge.org/mailman/listinfo/rspec-users
>
> _______________________________________________
> rspec-users mailing list
> rspec-us... at rubyforge.orghttp://rubyforge.org/mailman/listinfo/rspec-users


More information about the rspec-users mailing list