rspec / rspec-core

RSpec runner and formatters
http://rspec.info
MIT License
1.23k stars 765 forks source link

Arguments of shared example evaluated before any `before` block #2947

Closed YarikST closed 2 years ago

YarikST commented 2 years ago

Subject of the issue

Arguments of shared example evaluated before any before block. We should mention this in docs

Your environment

Steps to reproduce

describe 'ArgumentsOfSharedExamples' do
  before do
    puts "This line appears once arguments are evaluated"
    puts Time.current
    travel_to Time.current.middle_of_day.utc
  end

  after { travel_back }

  shared_examples 'rspec-examples' do |run_date|
    it 'should be the middle of day' do
      expect(run_date).to eq Time.current.middle_of_day
    end
  end

  puts "Evaluated arguments"
  puts Time.current
  include_examples 'rspec-examples', Time.current.middle_of_day
end
JonRowe commented 2 years ago

Hi, This is just normal Ruby precendence. Things outside of specs are evaluated before specs are run, its no different to how "some_value" here is evaulted before the name method is called.

class SomeClass
  some_value = Time.current.middle_of_day

  def name
    Time.current.middle_of_day
  end
end
SomeClass.new.name