How to include helpers in RSpec for different use cases

Updated . Posted . Visible to the public.
module FixtureHelper

  def fixture_base_path
    Pathname.new(File.expand_path('../../../fixtures', __FILE__))
  end

  def fixture_fake_repo_path
    Pathname.new(File.expand_path('../../../fixtures/fake_repo', __FILE__))
  end

end

RSpec.configure do |config|
  config.extend(FixtureHelper) # <--- make it available outside of an example (e.g. for `let`)
  config.include(FixtureHelper) # <--- make it available inside an example (e.g. `it` block)
end

require it in spec_helper.rb.


module SpecialHelper
  
  def rarely_used_function
    # ...
  end
  
end

RSpec.configure do |config|
  config.include(SpecialHelper, type: 'special') # <--- make it available only for `type: 'special'` blocks
end

require it in spec_helper.rb.
Use it like this:

RSpec.describe MyClass, type: 'special' do
  # ...
end
Judith Roth
Last edit
Judith Roth
Posted by Judith Roth to Judith's Dev Notes (2021-11-10 14:20)