Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

let() value cached across examples when before(:all) is used?

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

like image 200
Dao Xiang Avatar asked Oct 22 '12 04:10

Dao Xiang


1 Answers

This is a known problem with rspec:

lets are not designed to be used with before(:all) blocks

(Quoting rspec contributor myronmarston from yet another ticket on the issue (which seems quite similar to the behavior you're describing here)).

like image 102
pje Avatar answered Oct 20 '22 21:10

pje