Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 856
Jonas Söderström Avatar asked May 19 '10 12:05

Jonas Söderström


1 Answers

I think this is the default behavior for rspec-rails controller tests, in the sense that you can set an expectation on the response status and/or path, and test for success.

For example:

it "should create an account" do   post :create   response.code.should == "302"   response.should redirect_to(accounts_path) end 
like image 53
zetetic Avatar answered Sep 23 '22 15:09

zetetic