0

I want to create a custom variable similar to response object that should only be available in controller specs. I noticed that rspec supports filters which are before/after hooks which means I can create instance variables with them to be used later. But response object feels and works more like a let variable that is lazily evaluated. Also, controller specs support assign method that can accept arguments. Does rspec support any way to create similar methods to be used with a specific type of spec?

Note: I don't need to support anything below rspec 3.0.

Ansh
  • 67
  • 1
  • 3
  • 9

1 Answers1

2

You can simply do this by creating a module with your function and then including that in your RSpec configure block. You can control the types of specs where this should be available as a second parameter when you include the module:

module ControllerSpecHelpers
  def something
    'fubar2000'
  end
end

RSpec.configure do |config|
  config.include ControllerSpecHelpers, type: :controller
end

RSpec.describe BlahController, type: :controller do
  it 'should be possible to use the `something` helper in a controller spec' do
    expect(something).to eq('fubar2000')
  end
end
Patrick Oscity
  • 53,604
  • 17
  • 144
  • 168