Skip to content

Instantly share code, notes, and snippets.

@rsutphin
Created November 10, 2011 04:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsutphin/1354150 to your computer and use it in GitHub Desktop.
Save rsutphin/1354150 to your computer and use it in GitHub Desktop.
RSpec: `let`s leak across examples when invoked from before(:all)
puts "rspec-core #{RSpec::Core::Version::STRING}"
class InstantiationCounter
def initialize(k)
$count ||= { }
$count[k] ||= 0
$count[k] += 1
@k = k
end
def count
$count[@k]
end
end
describe 'Foo' do
if ENV['BEFORE_ALL']
before(:all) do
puts "Before all bar: #{bar.count}"
end
end
let(:bar) { InstantiationCounter.new('bar') }
let(:baz) { InstantiationCounter.new('baz') }
it 'is in the first one' do
puts "Bar: #{bar.count}"
puts "Baz: #{baz.count}"
end
it 'is in the second one' do
puts "Bar: #{bar.count}"
puts "Baz: #{baz.count}"
end
it 'is in the third one' do
puts "Bar: #{bar.count}"
puts "Baz: #{baz.count}"
end
end
# Output from
# $ BEFORE_ALL=y rspec foo_spec.rb -fn
rspec-core 2.8.0.rc1
Foo
Before all bar: 1
Bar: 1
Baz: 1
is in the first one
Bar: 1
Baz: 1
is in the second one
Bar: 1
Baz: 1
is in the third one
Finished in 0.00096 seconds
3 examples, 0 failures
# Output from
# $ rspec foo_spec.rb -fn
rspec-core 2.8.0.rc1
Foo
Bar: 1
Baz: 1
is in the first one
Bar: 2
Baz: 2
is in the second one
Bar: 3
Baz: 3
is in the third one
Finished in 0.00082 seconds
3 examples, 0 failures
@rsutphin
Copy link
Author

Example output is from 2.8.0.rc1, but the same behavior is present with all 2.x versions (checked 2.0.1, 2.1.0, 2.3.2, 2.4.0, 2.5.2, 2.6.4, and 2.7.1).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment