Search code examples
ruby-on-railsrspectddrspec-rails

rspec Let() behavior in helper specs


I have the following in my application_helper_spec.rb

shared_examples_for "some shared process" do

    it "displays the value defined by Let" do
        puts object.inspect # This should output the value defined by Let
    end

end

describe ApplicationHelper do

    describe "a_helper_methond" do

        describe "some behaviour" do
            let(:object) { "foo" }
            puts object.inspect # This should output the value defined by Let
            it_should_behave_like "some shared process"
        end

    end

end

However, when I run this I get two errors on both puts:

undefined local variable or method `object' for #<Class:0x007f951a3a7b20> (NameError)

Why? I have the exact same code in my model and request specs at it runs fine, but in the helper specs it doesn't.


Solution

  • The let call defines a method in the context your specs will be executed. Your puts statement however is outside that scope. You need to wrap it inside an it block.

    it 'print debug output' do
      puts object.inspect
    end