[rspec-users] Reusing story snippets

Kyle Hargraves philodespotos at gmail.com
Sat Jun 14 06:45:34 EDT 2008

On Sat, Jun 14, 2008 at 5:03 AM, Mikel Lindsaar <raasdnil at gmail.com> 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
> 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?

If it's implicit in almost every story, just call the helper in the
first step of your scenarios. This might be an issue if you're
striving to recycle steps, but otherwise it works perfectly well. If
the story obviously requires authentication, why bother specifying it?

Maybe others will see it differently, but I omit a lot of the
"scaffolding" preconditions when it's clear from context:

  Given a user editing their account details

Of course they have to be logged in for that. I may write a single
scenario that shows users can't get to their account details page
without being logged in, but the rest can happily just imply it:

  Given("a user editing their account details") do
    browse_to('account details page')


More information about the rspec-users mailing list