Ruby Forum RSpec > Reuse steps like a method call

Posted by Bryan Helmkamp (Guest)
on 14.05.2008 19:45
(Received via mailing list)
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,
Posted by Ben Mabey (mabes)
on 14.05.2008 19:59
(Received via mailing list)
Bryan Helmkamp wrote:
>   # 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
Posted by David Chelimsky (Guest)
on 14.05.2008 20:38
(Received via mailing list)
On May 14, 2008, at 12:45 PM, Bryan Helmkamp wrote:

>  # create a blog post
> end

I'm pretty sure you can do this:

Given "a user named $username with a blog post |username|
  Given "a user named #{username}"
  # create a blog post
end

Cheers,
David
Posted by Zach Dennis (Guest)
on 14.05.2008 22:12
(Received via mailing list)
On Wed, May 14, 2008 at 2:24 PM, David Chelimsky <dchelimsky@gmail.com>
wrote:

>> Given "a user named $username with a blog post |username|
> end
>

Is this by intention or just a side effect of the current 
implementation?
Posted by David Chelimsky (Guest)
on 14.05.2008 22:19
(Received via mailing list)
On May 14, 2008, at 3:07 PM, Zach Dennis wrote:

> Is there a way to call it from another step? For example:
>  Given "a user named #{username}"
>  # create a blog post
> end
>
> Is this by intention or just a side effect of the current  
> implementation?

You'll have to ask Dan - Dan?