I am creating controller tests for my application. I am just trying to test the index action for my maps_controller
.
I created this test for the index to see if it renders.
require 'rails_helper'
RSpec.describe MapsController, type: :controller do
describe 'GET #index' do
it 'shows a list of all maps' do
get :index
expect(response).to redirect_to maps_path
end
end
end
However when I run this spec test I get this error message.
Failure/Error: expect(response).to redirect_to maps_path
Expected response to be a redirect to http://test.host/maps but was a redirect to http://test.host/login. Expected "http://test.host/maps" to be === "http://test.host/login".
I can understand this because it does require a user to get to this path. However, I am unsure why this is trying to go to http://test.host/maps instead of localhost:3000/maps
. I was unable to find anything in RSpec docs about setting the test url. How can I set this to go to localhost:3000
instead of test.host
?
You can set it globally in your spec_helper.rb
file:
Since your spec is set to type: :controller
, you would do it this way:
RSpec.configure do |config|
# other config options
config.before(:each) do
@request.host = "localhost:3000"
end
end
However, as of RSpec 3.5 the preferred method of testing controllers is with a :request
spec. So if possible, change your spec to type: :request
and then you would do it this way:
RSpec.configure do |config|
# other config options
config.before(:each) do
host! "localhost:3000"
end
end