[rspec-users] Reuse steps like a method call

Ben Mabey ben at benmabey.com
Wed May 14 13:53:29 EDT 2008


Bryan Helmkamp wrote:
> If I have a step matcher defined as so:
>
> Given "a user named $username" do |username|
>  ....
> end
>
> Is there a way to call it from another step? For example:
>
> Given "a user named $username with a blog post |username|
>   given_a_user_named(username) # calls existing step code
>   # create a blog post
> end
>
> Cheers,
>
>   
I'm not sure if you can call a step directly... I would be very 
interested in that if you can.. But AFAIK the only option is to extract 
it into a helper method an place it in Spec::Story::World.

So you would have:

Given "a user named $username" do |username|
 create_user_with_name(user_name)
end

Given "a user named $username with a blog post |username|
  create_user_with_name(user_name)
  # create a blog post
end

I typically do things with instance variables though.  So the "Given a user named $username" step would set an instance variable named @username.
Then I would have a step that reads "$username has a blog post" that would get the instance variable name and it would build a post off of that user.  The story would the read like:

Given a user named Joey
And Joey has a blog post 


-Ben


More information about the rspec-users mailing list