Skip to content

Instantly share code, notes, and snippets.

@YumaInaura
Last active July 15, 2018 08:47
Show Gist options
  • Save YumaInaura/87bcb2aa79ee0eb9ba2a2cf048025ab8 to your computer and use it in GitHub Desktop.
Save YumaInaura/87bcb2aa79ee0eb9ba2a2cf048025ab8 to your computer and use it in GitHub Desktop.
In RSpec before and after hooks, :context is same :all, :example is same as :each

Rspec ruby code

# https://relishapp.com/rspec/rspec-core/v/3-7/docs/hooks/before-and-after-hooks

RSpec.configure do |config|
  config.before :context do
    puts 'before context'
  end

  config.before :example do
    puts 'before example'
  end

  # Note: the :example and :context scopes are also available as :each and :all, respectively. Use whichever you prefer.

  # Same as context
  config.before :all do
    puts 'before all'
  end

  # Same as example
  config.before :each do
    puts 'before each'
  end
end

RSpec.describe do
  subject { true } 
 
  describe do
    it { is_expected.to be true }
    it { is_expected.to be true }
  end

  describe do
    it { is_expected.to be true }
    it { is_expected.to be true }
  end
end

Run rspec result

$ bundle exec rspec -fd rspec/before_hooks.rb

before context
before all

before example
before each
    should equal true
before example
before each
    should equal true

before example
before each
    should equal true
before example
before each
    should equal true

Finished in 0.00986 seconds (files took 0.29761 seconds to load)
4 examples, 0 failures

Versions

  • ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]
  • rspec-core 3.7.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment