Unexpected behavior of let() and before(:all) #656

hosh opened this Issue Jul 30, 2012 · 1 comment

2 participants


Ever since the Rspec 2.0 beta during the big Rails 3 refactoring, I have been extensively using let(). I have also consistently run into a problem with using let() with before(:all) blocks. I'm surprised no one has reported this as a bug. I am reporting this once and for all to get an answer. This is a test that will break it in Rspec 2.11.0, but I have seen this behavior as early as rspec 2.1.x

If you comment out the before(:all) block, all tests will pass. As soon as before(:all) references anything in let(), then both examples will fail -- even for let() that are not directly referenced within the before(:all) block. I am thinking this has something to do with how let() memoize return values, probably related to where it is storing it in class variables.

Is this expected behavior?

If this is expected behavior, please explain what the best practice workaround for this is.
If this is not, how does one fix it?

describe 'testing let' do
  context 'foo' do             
    before(:all) { puts "before(:all) foo = #{foo}" }

    let(:foo) { :parent }
    let(:bar) { :parent }      

    context 'foo 1' do
      let(:foo) { :current }   
      let(:bar) { :current }   

      it 'should use the current foo' do 
        foo.should eql :current
        # This fails, why?

      it 'should use the current bar' do 
        bar.should eql :current
        # This also fails. This isn't even called in before(:all), so why should this fail?

    it 'should use parent foo' do   
      foo.should eql :parent

    it 'should use parent bar' do   
      bar.should eql :parent

RSpec member

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

See #573 and #500 for the prior discussion about this issue.

I'm closing this to reduce the number of duplicate conversations--please continue the conversation (if you have further questions/comments) on one of those.

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