How do i set a request.referrer inside my RSpec?
Mock it:
specify 'foo' do
controller.request.should_receive(:referer).and_return('http://example.com')
# get whatever
end
Or if you don't care if it doesn't get called, stub it:
controller.request.stub referer: 'http://example.com'
How to set the referrer in an _integration_ test
Capybara.current_session.driver.header 'Referer', 'http://example.com'
It looks like you're using Capybara, you can use Capybara to explicitly set the referer. You would have to update it whenever you wanted it to change, and if you needed to remove it you could set it to nil
.
Maybe a bit cleaner:
referer = 'http://example.com'
Capybara.current_session.driver.header 'Referer', referer
Rails using request.referer in testing
Two things:
First, this answers your main question:
How do I set HTTP_REFERER when testing in Rails?
Second, it's not a given that request.referer will be set. Most browsers supply the header when you navigate from a previous page; most don't when you hand-enter a URL. HTTP clients can't be assumed to do so overall, and you have to be prepared to get nil from that attribute.
How do I set HTTP_REFERER when testing in Rails?
Their recommendation translates to the following:
setup do
@request.env['HTTP_REFERER'] = 'http://test.com/sessions/new'
post :create, { :user => { :email => 'invalid@abc' } }
end
How to set HTTP_REFERER in cucumber+capybara step definitions?
Since I'm using Cucumber::RackTest::Driver, the solution for me at the time of writing is:
Capybara.current_session.driver.header 'Referer', 'http://example.com'
This solution was inspired by the following unmerged pull request on capybara:
https://github.com/thoughtbot/capybara-webkit/issues/198
Related Topics
Memory Size of a Hash or Other Object
Import SASS Partial Over Http Instead of Filesystem
Passenger: Cannot Load Such File Rubygems/Builder
How to Globally Ignore Invalid Byte Sequences in Utf-8 Strings
Rspec Testing Has_Many :Through and After_Save
Rails Custom Validation - Only One Record Can Be True
Custom Rails Configuration Section
Trouble on Rendering a Template Passing a Local Variable
To Make an API Call with an Oauth Token
"Use" Keyword/Word in Ruby/Rails/Rack Code
How to Pluck Email from Array of Users
How to Use the Ruby "Self" Keyword
How to Save a Timezone Correctly with Ruby and Mongoid
Sending a Delete Request from Sinatra
Connecting to Oracle Db Using Ruby
Simple Regex -- Replace Underscore with a Space
Require Lib in Rspec with Ruby 1.9.2 Brings "No Such File to Load"