Calling multiline steps from other steps


#1

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.


#2

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.


#3

Ben M. wrote:

you will just have to
experiment and see.

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

Anyone else have any ideas?

Paul


#4

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


#5

Ben M. wrote:

you will just have to
experiment and see.

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

Anyone else have any ideas?

Not possible yet. Please file a ticket.


#6

And please link to this thread in the ticket. Nabble or somesuch.


#7

Andrew P. wrote:

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

> > 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”.
:slight_smile: Read about them here:
http://github.com/aslakhellesoy/cucumber/wikis/using-fit-tables-in-a-feature

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


#8

Ooh interesting stuff thanks for that Ben, never got past the scenario
tables until now :slight_smile:

Andrew

2008/12/5 Ben M. removed_email_address@domain.invalid


#9

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-cucumber/tickets/144-call-multiline-steps-from-other-steps