0

I have a couple same tests, the only different is the arguments, but I am having trouble of looping them one by one

Here is an example where the test is

subject.orange['orange tray'] do |tray|   
    expect(tray).not_to be_nil

subject.apple['apple tray'] do |tray|   
    expect(tray).not_to be_nil

here is what I am trying to do

fruits_list = ['orange', 'apple']

fruits_list.each do |fruit|
  subject."#{fruit}[#{fruit} tray]" do |tray|    <------- the error, I can't get this to work
    expect(tray).not_to be_nil
end

1 Answer 1

3

You need to use the send method, not just hand a string to an object. You could probably do something like:

['orange', 'apple'].each do |fruit|
  subject.send(fruit.to_sym)["#{fruit} tray"] do |tray|
    expect(tray).not_to be_nil
  end
end
Sign up to request clarification or add additional context in comments.

5 Comments

thank you so much.. wasn't sure what keywords to search for for this and tried multiple things...
No problem! About a month ago I took a quick glance through the object, hash, and array documentations and have experienced a lot of benefits since. They are, respectively, ruby-doc.org/core-2.1.0/Object.html, ruby-doc.org/core-2.1.0/Hash.html, and ruby-doc.org/core-2.1.0/Array.html.
Thank you I will definitely check those out
what about subject.exist[:orange_tray] it's not something like subject.exist[:#{fruit}_tray]
What? That comment doesn't make sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.