1

I'm having scope issues when using RSpec's `before(:all)' block.

Previously I was using before(:each), which worked fine:

module ExampleModule

    describe ExampleClass

        before(:each) do

            @loader = Loader.new

        end

        ...


       context 'When something' do


            before(:each) do
                puts @loader.inspect # Loader exists
                # Do something using @loader
            end

            ...

        end

    end

end

But switching the nested before(:each) block tobefore(:all) means loader is nil:

module ExampleModule

    describe ExampleClass

        before(:each) do

            @loader = Loader.new

        end

        ...


        context 'When something' do


            before(:all) do
                puts @loader.inspect # Loader is nil
                # Do something using @loader
            end

            ...

         end

    end

end

So why is @loader nil in the before(:all) block, but not in the before(:each) block?

4

2 に答える 2