Posted over 9 years ago. Visible to the public.

Spreewald: When using `patiently do`, don't reuse existing variable names

Spreewald Archive 's patiently repeats the given block again and again until it either passes or times out.

Be careful to give patiently a block that can actually be repeated. E.g. the following block can not be repeated:

Copy
Given /^the field "(.*?)" is empty$/ do |field| patiently do field = find_field(field) field.text.should be_blank end end

The reason the above code will fail when repeated is that the block is reassigning the field variable that was defined outside of the block (it's the variable set from the Given block's arguments). Because of this, during the first repetition the value of field will actually be:

Copy
field = find_field(find_field(field))

And during the second repetition:

Copy
field = find_field(find_field(find_field(field)))

To not fall into this trap, do not reuse variable names that are defined outside the repeated block. To fix the example above, we will use two separate variables label and field:

Copy
Given /^the field "(.*?)" is empty$/ do |label| # ← patiently do field = find_field(label) field.text.should be_blank end end

Note that you can of course reuse the names of variables that have been defined inside the repeated block.

Growing Rails Applications in Practice
Check out our new e-book:
Learn to structure large Ruby on Rails codebases with the tools you already know and love.

Owner of this card:

Avatar
Henning Koch
Last edit:
over 9 years ago
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more