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.

Inherit and extend let definitions in inner describe blocks. #294

@Peeja

Description

@Peeja

I've taken to an idiom like the following:

describe Post do
  subject { Post.new(post_attributes) }
  let(:post_attributes) { { } }

  its(:description) { should be_empty }

  describe "with a title" do
    let(:post_attributes) { { title: "10 things about yak shaving you're doing wrong" } }

    its(:description) { should == "10 things about yak shaving you're doing wrong" }

    describe "with an author" do
      let(:post_attributes) { { title: "10 things about yak shaving you're doing wrong", author: "Ernest Holbrecht" } }

      its(:description) { should == "10 things about yak shaving you're doing wrong by Ernest Holbrecht" }
    end
  end
end

The Post.new call is DRYed up into the top of the spec, and each nested describe block defined what's different from its parent block. Except: here, the innermost describe block's definition of post_attributes repeats the title from the one above it.

I'd prefer to say something like this:

context "with an author" do
  let(:post_attributes) { super.merge( author: "Ernest Holbrecht" ) }

  its(:description) { should == "10 things about yak shaving you're doing wrong by Ernest Holbrecht" }
end

Unfortunately, I don't think we can use super, it would have to be super() (which is uglier) or else we'd get the error "Implicit argument passing of super from method defined by define_method() is not supported. Specify all arguments explicitly."

So this issue is here to decide two things:

  1. Is this a good thing to add to RSpec?
  2. If so, what's the right word to use to refer to the inherited value of a let?

I'm happy to write the implementation.

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