Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do i set a request.referrer inside my RSpec?

I am trying to avoid using my session[:referred_by], and would like to use the request.referrer. However, my RSpec tests fail because the TestRequest does not store a request.referrer

So I have to do the following in order for Rspec tests to work. Is there a way to make it better:

referrer = request.referrer ? request.referrer : '/' redirect_to referrer, :alert => error_message 
like image 555
Kamilski81 Avatar asked Sep 25 '12 22:09

Kamilski81


People also ask

How do I set up RSpec?

Installing RSpecBoot up your terminal and punch in gem install rspec to install RSpec. Once that's done, you can verify your version of RSpec with rspec --version , which will output the current version of each of the packaged gems. Take a minute also to hit rspec --help and look through the various options available.

What is RSpec Rails gem?

RSpec is a testing framework written in Ruby to test Ruby code. To get started using RSpec with Rails, add it to the Gemfile. group :development, :test do gem 'rspec-rails' end. Next finish setting it up by running bundle install in your project directory and then. rails generate rspec:install.

What is Ruby RSpec?

RSpec is a testing tool for Ruby, created for behavior-driven development (BDD). It is the most frequently used testing library for Ruby in production applications. Even though it has a very rich and powerful DSL (domain-specific language), at its core it is a simple tool which you can start using rather quickly.


1 Answers

ActionDispatch::TestRequest extends ActionDispatch::Request that extends Rack::Request.

The method is defined as follows

def referer   @env['HTTP_REFERER'] end alias referrer referer 

As far as I remember, you can access the environment variable in the RSpec test by using request.env. It means, it should be possible to set something like

request.env['HTTP_REFERER'] = 'http://example.com' 

Of course, it depends on the type of RSpec example group you are using.

like image 174
Simone Carletti Avatar answered Oct 02 '22 01:10

Simone Carletti