Passing a variable from a .feature file as an array argument

I am trying to pass parameters from a .feature file (cucumber) to a method call in a .rb heres file, which I have:

When I set something to blah 

step definition for the above function file:

  When /^I set something to (.+)$/ do |value| methodcall(["#{value}"])) end 

in a separate .rb file

  def methodcall (a=[],b=[],c=[]) a.each {|x| do something with x } b.each {|y| do something with y } c.each {|z| do something with z } ... end 

It works fine when I give only one parameter in the .feature file. I hope it will work for a single value for each of the array (haven't tried this though)

but what I want to do is write something like this in the function file

  And I set something to "blah,blah1,blah2" and somethingelse to "blah4,blah5" and otherthings to "blah6, blah7" 

for the above, the step definition will look like

  When /^I set something to (.+) and somethingelse to (.+) and and otherthings to (.+)$/ do |value, value1, value2| methodcall(["#{value}"], ["#{value1}"], ["#{value2}"])) end 

and as a result of determining the above step, the values ​​in the value, value1, value2 are passed in the arguments corresponding to a = [], b = [], c = [], and the methodcall () method is called. someone can help .. thanks.

+4
source share
1 answer

In your code

 methodcall(["#{value}"]) 

You take a string value and turn it into the first element of the array, which is then passed to the method () method.

I believe that you really want to do

 methodcall(value.split(',')) 

It will take your value, break it with commas and put each of them as an array element.

To illustrate your example, given that methodcall ():

 def methodcall (a=[],b=[],c=[]) a.each {|x| puts x } b.each {|y| puts y } c.each {|z| puts z } end 

Then

 value = "blah,blah1,blah2" methodcall(["#{value}"]) #=> blah,blah1,blah2 

Where

 value = "blah,blah1,blah2" methodcall(value.split(,)) #=> blah #=> blah1 #=> blah2 
+3
source

Source: https://habr.com/ru/post/1396303/


All Articles