Created
November 10, 2011 04:53
-
-
Save rsutphin/1354150 to your computer and use it in GitHub Desktop.
RSpec: `let`s leak across examples when invoked from before(:all)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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).