module RSpec::LetAs

Constants

VERSION

Public Instance Methods

let_as(name) click to toggle source
# File lib/rspec/let_as.rb, line 11
      def let_as(name)
        example_group = binding.callers.lazy.map(&:receiver).find {|receiver|
          receiver.is_a?(Class) && receiver.ancestors.include?(RSpec::Core::ExampleGroup)
        }

        raise Error, <<~EOS unless example_group
          `let_as` must be called from an example group (describe / context) or its inner block.

              RSpec.describe 'answer' do
                before do
                  42.let_as :answer
                end

                example do
                  expect(answer).to eq(42)
                end
              end
        EOS

        tap {|val|
          example_group.let(name) { val }
        }
      end