Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Shared contexts that access a let method in a before hook prevent other let methods from resetting #624

@d11wtq

Description

@d11wtq

I've been running down a rabbit hole for an hour or so and have figured out that RSpec is behaving badly when the following conditions are true.

  1. You have a shared context that defines a let method.
  2. That shared context accesses the let method in a before hook.
  3. The example group in which the shared context is included creates a new let, with a value derived from the shared let.

If all of the above are true, RSpec doesn't seem to clear the let that is defined in the example group that is including the shared context. If that makes no sense, the following spec fails in the "value in second context" block, stating that the value was 42, not 7. That's only possible if RSpec hasn't reset the let with the same name in the context immediately before it. I think I have distilled this down as much as I can, but if I can trim it down further, I'll update this issue.

shared_context "something that uses a 'let' in a before hook" do
  before(:all) do
    shared_let[:x] = 11 # access the shared let in some way
  end

  let(:shared_let) do
    { :a => 42, :b => 7}
  end
end

describe "broken shared contexts" do
  include_context "something that uses a 'let' in a before hook"

  describe "value in first context" do
    let(:value) { shared_let[:a] }

    it "has a value of 42" do
      value.should == 42
    end
  end

  describe "value in second context" do
    let(:value) { shared_let[:b] }

    it "has a value of 7" do
      value.should == 7
    end
  end
end
Failures:

  1) broken shared contexts value in second context has a value of 7
     Failure/Error: value.should == 7
       expected: 7
            got: 42 (using ==)
     # ./spec/shared_context_bug_spec.rb:28:in `block (3 levels) in <top (required)>'

.... SNIP ....

[chris@chipbook:~/coding/rspec_bug%] grep rspec Gemfile.lock
    rspec (2.10.0)
      rspec-core (~> 2.10.0)
      rspec-expectations (~> 2.10.0)
      rspec-mocks (~> 2.10.0)
    rspec-core (2.10.1)
    rspec-expectations (2.10.0)
    rspec-mocks (2.10.1)
  rspec

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions