redirect_back fallback_location root_path rspec code example

Example 1: redirect_back fallback_location root_path rspec

RSpec.describe InterestsController, type: :controller do
  describe 'POST #create' do
    context 'with valid attributes' do
      it 'redirects to referer' do
        from about_path
        post :create, params: { interest: attributes_for(:interest) }
        expect(response).to redirect_to about_url
      end
    end
  end
end

Example 2: redirect_back fallback_location root_path rspec

RSpec.configure do |config|
  config.include RedirectBack
end

Example 3: redirect_back fallback_location root_path rspec

module RedirectBack
  def from(url)
    request.env['HTTP_REFERER'] = url
  end
end