error of testing redirect in controller

Hi,

I want to test the user controller, how can i do

the code is

  if @user.save       redirect_to :action => "confirm" else .....

test user = User.new(:email => "blad@mail.com",                       :password => "12345",           :name => "you")       post :create user.should_receive(:save).and_return(true)       response.should redirect_to('confirm')

the error

  1) UsersController GET create should save redirect to confirm      Failure/Error: response.should redirect_to(:action => 'confirm')        Expected response to be a <:redirect>, but was <200>.        Expected block to return true value.

please help

You have to send params along with call to post.

Since there are no params in post, it goes to the else section of you controller, where you render a action, and response code for render is 200. So the test fails saying that you expected a redirect but got render.

Chirag http://sumeruonrails.com

You have to send params along with call to post. Since there are no params in post, it goes to the else section of you controller, where you render a action, and response code for render is 200. So the test fails saying that you expected a redirect but got render.

Can you give me an example that how to send the param to the post. I am pretty new with testing controller in rails

Your test can be something like this:

post :create, :user => {:email => “test_email@mail.com”, :password => “12345”, :name => “you”}

response.should redirect_to(‘confirm’)

Chirag http://sumeruonrails.com

You may also want to read through Rails guides on testing - http://guides.rubyonrails.org/testing.html#functional-tests-for-your-controllers

Chirag http://sumeruonrails.com

Your test can be something like this:

post :create, :user => {:email => "test_em...@mail.com", :password => "12345", :name => "you"} response.should redirect_to('confirm')

Thank you Chirag, it s good now