Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RSpec should redirect_to problem

I'm writing a spec for my Rails controller, this is the action I'm testing:

def create
  @course = Course.new(params[:course])
  if @course.save then
    #flash[:notice] = 'Course Created'
    redirect_to courses_path
  else
    render :action => 'new', :status => 400
  end
end

And this is the spec that validates it:

describe "POST /courses [Good Input]" do

  it "should redirect to Courses index page after creation" do
    @course.stub!(:save).and_return(true)
    post :create
    response.should be_success
    response.should redirect_to(courses_path)
  end

end

Still I'm getting this error from RSpec:

'CoursesController POST /courses [Good Input]

should redirect to Courses index page after creation'

FAILED

expected redirect to "/courses", got no redirect

Any ideas why this happens?

SOLVED

as rishavrastogi stated, should be_success expects a http code on the 2xx range, and a redirect falls into the 3xx range (actually its 302)

The assertion needs to be changed to => response.should be_redirect.

Although in this case, it's redundant to check that the response is a redirect and then checking that redirects to an specific page, so that assertion is not needed anymore.

like image 312
Pablo Fernandez Avatar asked Mar 15 '09 16:03

Pablo Fernandez


1 Answers

I'm not a RSpec-er either but I guess "response.should be_success" shouldn't be there because the response is actually a "HTTP redirect" not "HTTP success" ... so try removing response.should be_success

Also change

 post :create 

to

 post :create, :course => {} 
like image 93
Rishav Rastogi Avatar answered Oct 15 '22 01:10

Rishav Rastogi