I have a rspec test testing a controller action.
class SalesController < ApplicationController
# This redirect sends the user to SalesController#go_to_home
RSpec.describe SalesController, type: :controller do
describe 'POST create' do
expect(response).to redirect_to '/'
Expected response to be a redirect to <http://test.host/> but was a redirect to <http://test.host/go_to_home>.
Expected "http://test.host/" to be === "http://test.host/go_to_home".
Controller tests are effectively unit tests - you are testing the effect of calling a single action and what the expected behaviour of that action is.
create action does return a response back with a status code of
302 and includes in the header a
Location indicating the new URI, which in the case of calling create would be
This is as far as the controller test goes. It has emulated a call made from a browser to the create action and received the initial redirection.
In the real world of course the browser would then navigate to the given location and would then hit the
go_to_home action but this is beyond the scope of controller tests ... this is in the domain of integration testing.
createaction, follow the redirection and test that you end up at '/'.
expect(response).to redirect_to '/go_to_home'
createaction to redirect directly to '/'