Search code examples
ruby-on-railstestingrspecweb-testing

Rspec - Rails - How to follow a redirect


Does anyone know how to make rspec follow a redirect (in a controller spec)? (e.g test/unit has follow_redirect!)

I have tried "follow_redirect!" and "follow_redirect" but only get

undefined method `follow_redirect!' for #<Spec::Rails::Example::ControllerExampleGroup::Subclass_1:0xb6df5294>

For example:
When I create an account the page is redirected to accounts page and my new account should be at the top of the list.

it "should create an account" do
  post :create, :name => "My New Account"
  FOLLOW_REDIRECT!
  response.code.should == "200"
  accounts = assigns[:accounts]
  accounts[0].name.should == "My New Account"
end

But FOLLOW_REDIRECT! needs to be changed to something that actually works.


Solution

  • If you want to test the redirect you are moving outside of the rspec-rails domain.

    You can use Webrat or some other integration-test framework to test this.

    The easiest way to solve this without resorting to integration testing is probably to mock out the method that is causing the redirect.