Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

RSpec: `let`s leak across examples when invoked from before(:all)

View foo_spec.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
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
View foo_spec.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# 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
View foo_spec.rb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
# 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
Owner

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
Something went wrong with that request. Please try again.