Forum: RSpec calling multiline steps from other steps

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Pau C. (Guest)
on 2008-12-05 04:36
What is the syntax for calling multiline steps from other steps? I'd
like to do something like this:

Given /^I have filled in the form$/ do |details|
  details.hashes.each |pair|
    When "I fill in #{pair['field']} with #{pair['value']}"
  end
end

Given /^(.*) has filled in the form$/ do |user, details|
  Given "I am logged in and #{user}"
  Given "I have filled in the form", details
  When "I log out"
end

My feature file would have something like this:

  Given Joe has filled in the form:
    | field | value |
    | x     | 1     |
    | y     | 2     |


I'd also like to do something like this:

Given /^Everything is filled in$/ do
  Given "Joe has filled in the form:
    | field | value |
    | x     | 1     |
    | y     | 2     |"
end


How can I do this? Also, how does it work for multi line strings (""")?

Thanks!
Paul

P.S. Those are contrived examples.
Ben M. (Guest)
on 2008-12-05 05:33
(Received via mailing list)
Pau C. wrote:
> What is the syntax for calling multiline steps from other steps? I'd
> like to do something like this:
>
> Given /^I have filled in the form$/ do |details|
>   details.hashes.each |pair|
>     When "I fill in #{pair['field']} with #{pair['value']}"
>   end
> end
>
>
Yes, you can do this.
>     | y     | 2     |
>
>
I'm not sure if it works with step tables, so you will just have to
experiment and see.  Same thing for the multi-line strings.. I'm not
sure but that seems more possible that the step table.
Pau C. (Guest)
on 2008-12-05 07:59
Ben M. wrote:
> you will just have to
> experiment and see.

Thanks for the reply!
So far all my experiments have failed :/

Anyone else have any ideas?

Paul
Andrew P. (Guest)
on 2008-12-05 08:30
(Received via mailing list)
Given /^I have filled in the form$/ do |details|
 details.hashes.each |pair|

Can't see this working because you've got no place to collect details

However

Given /^I have filled in the form with (.*)$/ do |details|
 details.hashes.each |pair|

would work, but you'd have to do some tricky stuff to get your hash out.

-----

 Given Joe has filled in the form:
   | field | value |
   | x     | 1     |
   | y     | 2     |

For this to work the step that matches has to collect two thing so
really
you need

 Given Joe has filled in foo with bar
   | field | value |
   | x     | 1     |
   | y     | 2     |

and a matcher

Given /^Joe has filled in (.*) with (.*)$/ do |field, value|

>From that you could try

Given /^Form filled in correctly$/
  Given "Joe has filled in foo with bar"
     | field | value |
     | x     | 1     |
     |   y   | 2     |

but I doubt that would work as I think you need the "More Examples:"
statement. Also don't think embedding tables in step definitions is a
good
idea.

----

You could use convention over configuration to do more filling in with
less
variables. For example I fill in user forms using just the login name to
generate the other fields

e.g. login - fred, password - fredpass, email 
removed_email_address@domain.invalid  etc.

Now a step can take one parameter fred and fill in many details

e.g. using webrat

When /^I fill in signup details for (.*) $/ do |user|
  fill_in(:login, :with => user)
  fill_in(:email, :with => user + '@example.com')
  fill_in(:password, :with => user + 'pass')
  fill_in('user[password_confirmation]', :with => user + 'pass')
end

When I fill in signup details for fred

HTH

Andrew








2008/12/5 Pau C. <removed_email_address@domain.invalid>
Ben M. (Guest)
on 2008-12-05 08:54
(Received via mailing list)
Andrew P. wrote:
> would work, but you'd have to do some tricky stuff to get your hash out.
>
<snip>
>
> but I doubt that would work as I think you need the "More Examples:"
> statement. Also don't think embedding tables in step definitions is a
> good idea.
>

Actually, you can embed tables in steps, they are called "Step Tables".
:)  Read about them here:
http://github.com/aslakhellesoy/cucumber/wikis/usi...

I have used them many times and I find them very handy.  I had rolled my
own with story runner and so having it built into cucumber makes things
very nice.

-Ben
Aslak H. (Guest)
on 2008-12-05 09:23
(Received via mailing list)
> Ben M. wrote:
>> you will just have to
>> experiment and see.
>
> Thanks for the reply!
> So far all my experiments have failed :/
>
> Anyone else have any ideas?
>

Not possible yet. Please file a ticket.
Aslak H. (Guest)
on 2008-12-05 09:25
(Received via mailing list)
And please link to this thread in the ticket. Nabble or somesuch.
Andrew P. (Guest)
on 2008-12-05 12:47
(Received via mailing list)
Ooh interesting stuff thanks for that Ben, never got past the scenario
tables until now :)

Andrew

2008/12/5 Ben M. <removed_email_address@domain.invalid>
Pau C. (Guest)
on 2008-12-22 22:56
Aslak Hellesøy wrote:
> And please link to this thread in the ticket. Nabble or somesuch.

I am sorry for taking so long to post the ticket:
http://rspec.lighthouseapp.com/projects/16211-cucu...
This topic is locked and can not be replied to.