Read more

Spreewald: patiently blocks must not change variables from the surrounding scope

Henning Koch
January 23, 2023Software engineer at makandra GmbH

I recently enjoyed debugging a Cucumber step that tried to be retryable using a patiently Show archive.org snapshot block:

Then /^"([^"]*)" should( not)? be selected for "([^"]*)"$/ do |value, negate, field|
  patiently do
    field = find(:label, text: field)['for'].delete_suffix('-ts-control')
    ...
  end
end
Illustration online protection

Rails Long Term Support

Rails LTS provides security patches for old versions of Ruby on Rails (2.3, 3.2, 4.2 and 5.2)

  • Prevents you from data breaches and liability risks
  • Upgrade at your own pace
  • Works with modern Rubies
Read more Show archive.org snapshot

Unfortunately this block is not retryable:

  • The first attempt changes the value of field.
  • All subsequent attempts will using the changed value of field, instead of the original step arguments.

All variables declared within a patiently block must be scoped to that block. The example above needs to be changed to something like this:

Then /^"([^"]*)" should( not)? be selected for "([^"]*)"$/ do |value, negate, label|
  patiently do
    # Note that the "field" value is now local and not shared between patiently retries
    field = find(:label, text: label)['for'].delete_suffix('-ts-control')
    ...
  end
end
Posted by Henning Koch to makandra dev (2023-01-23 22:19)