Assignment still proceeds even with undefined local variable or method
This week I had a really horrible bug. One spec in our suite in our CI knocked out every subsequent spec after skipping. The spec works in isolation. just not on CI and not in a full suite run.
RSpec.shared_contexts 'with some feature' do
skip('This is not available on CI') if ENV['CI']
original_driver = Capybara.javascript_driver
Capybara.javascript_driver = :something_else
ensure
Capybara.javascript_driver = original_driver
end
RSpec.describe 'something' do
include_context 'with some feature'
it do
visit '/'
end
end
Turns out our Capybara.javascript_driver
ended up being nil, and our test suite fell over because CI doesn’t support non-headless Chrome. And the reason…
I would appear that variable assignment in Ruby still assigns nil
if we attempt to assign a variable from another variable or method that is undefined.
irb(main):001> a = some_method_or_var_that_is_not_defined
(irb):1:in `<main>': undefined local variable or method `some_method_or_var_that_is_not_defined' for main (NameError)
a = some_method_or_var_that_is_not_defined
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
from <internal:kernel>:187:in `loop'
from /Users/rl/.asdf/installs/ruby/3.3.0/lib/ruby/gems/3.3.0/gems/irb-1.14.0/exe/irb:9:in `<top (required)>'
from /Users/rl/.asdf/installs/ruby/3.3.0/bin/irb:25:in `load'
from /Users/rl/.asdf/installs/ruby/3.3.0/bin/irb:25:in `<main>'
irb(main):002> a
=> nil
Who knew? I didn’t.