Methods

Class Index [+]

Quicksearch

RSpec::Core::Let::ExampleGroupMethods

Public Instance Methods

let(name, &block) click to toggle source

Generates a method whose return value is memoized after the first call. Useful for reducing duplication between examples that assign values to the same local variable.

@note `let` can enhance readability when used sparingly (1,2, or

  maybe 3 declarations) in any given example group, but that can
  quickly degrade with overuse. YMMV.

@note `let` uses an `||=` conditional that has the potential to

  behave in surprising ways in examples that spawn separate threads,
  though we have yet to see this in practice. You've been warned.

@example

  describe Thing do
    let(:thing) { Thing.new }

    it "does something" do
      # first invocation, executes block, memoizes and returns result
      thing.do_something

      # second invocation, returns the memoized value
      thing.should be_something
    end
  end
    # File lib/rspec/core/let.rb, line 31
31:         def let(name, &block)
32:           define_method(name) do
33:             __memoized.fetch(name) {|k| __memoized[k] = instance_eval(&block) }
34:           end
35:         end
let!(name, &block) click to toggle source

Just like `let`, except the block is invoked by an implicit `before` hook. This serves a dual purpose of setting up state and providing a memoized reference to that state.

@example

  class Thing
    def self.count
      @count ||= 0
    end

    def self.count=(val)
      @count += val
    end

    def self.reset_count
      @count = 0
    end

    def initialize
      self.class.count += 1
    end
  end

  describe Thing do
    after(:each) { Thing.reset_count }

    context "using let" do
      let(:thing) { Thing.new }

      it "is not invoked implicitly" do
        Thing.count.should eq(0)
      end

      it "can be invoked explicitly" do
        thing
        Thing.count.should eq(1)
      end
    end

    context "using let!" do
      let!(:thing) { Thing.new }

      it "is invoked implicitly" do
        Thing.count.should eq(1)
      end

      it "returns memoized version on first invocation" do
        thing
        Thing.count.should eq(1)
      end
    end
  end
    # File lib/rspec/core/let.rb, line 90
90:         def let!(name, &block)
91:           let(name, &block)
92:           before { __send__(name) }
93:         end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.