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

let! and subject! ordering #1021

Closed
docwhat opened this issue Jul 25, 2013 · 2 comments
Closed

let! and subject! ordering #1021

docwhat opened this issue Jul 25, 2013 · 2 comments

Comments

@docwhat
Copy link

docwhat commented Jul 25, 2013

It would be nice to have a feature/documentation for the ordering of let! and subject! since it can make a difference.

Personally, I'd prefer they were run last; after all before blocks and before the example.

e.g.

  Scenario: subject bang runs after all before blocks at latest
    Given a file named "subject_bang_last_spec.rb" with:
    """ruby
      describe "subject! runs last" do
        subject! { array.count }
        let(:array) { [] }
        before { array.push 1 }
        describe "nesting group" do
          before { array.push 2 }
          it { should eq(2) }
        end
      end
      """
    When I run `rspec subject_bang_last_spec.rb`
    Then the examples should all pass

It looks like, however, they are run first.

Ciao!

@myronmarston
Copy link
Member

subject! and let! are very implicit constructs, that, while occasionally useful, should not be abused, and I think that if you really need things to happen in a particular order you should explicitly write it so that it happens in that order:

it 'returns the correct count' do
  array = []
  array.push 1
  array.push 2
  count = array.count
  expect(count).to eq(2)
end

There's no ambiguity here, and no room for misinterpretation.

The particular ordering you are asking for (having a subject! defined in a parent context called before a before hook in a child context) doesn't make sense with the semantics of RSpec's before hooks: we've explicitly documented that parent before hooks run before child before hooks, and subject! is simply a subject declaration plus a before hook. Changing it to act like you are requesting would break many users, and would run counter to how most people expect it to work.

@JonRowe
Copy link
Member

JonRowe commented Jul 25, 2013

👍 To what myron said. You could also order them by calling lazy lets in a before block, or chaining them together.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants