I have a spec file which looks like:
# foo_spec.rb
class Foo
end
describe Foo do
let(:foo) { 'foo' }
subject { bar }
# before(:all) { foo } # The seond example fails if uncomment this line.
describe 'test one' do
let(:bar) { 'one' }
it { should == 'one' }
end
describe 'test two' do
let(:bar) { 'two' }
it { should == 'two' }
end
end
Both examples are passing as expected. However, if I un-comment the before(:all), the second example fails:
1) Foo test two
Failure/Error: it { should == 'two' }
expected: "two"
got: "one" (using ==)
AFAIK, the value of let() will be cached across multiple calls in the same example but not across examples. So not quite sure why it fails the second example when the before(:all) is used.
I'm using ruby 1.9.2p180 and rspec 2.6.4
This is a known problem with rspec:
let
s are not designed to be used withbefore(:all)
blocks
(Quoting rspec contributor myronmarston from yet another ticket on the issue (which seems quite similar to the behavior you're describing here)).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With