[rspec-users] Problems with redirect_to

Maurício Linhares mauricio.linhares at gmail.com
Fri Feb 22 09:10:32 EST 2008


Using the "path" method the spec passes, but i don't use the path
method on my controller. First, here's my controller code:

  def create
    @static_page = @event.static_pages.build(params[:static_page])

    respond_to do |format|
      if @static_page.save
        flash[:notice] = 'Page was successfully created.'
        format.html { redirect_to(:action => 'show', :id =>
@static_page.permalink ) } ## thats what i'm testing agains
        format.xml  { render :xml => @static_page, :status =>
:created, :location => @static_page }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @static_page.errors, :status =>
:unprocessable_entity }
      end
    end
  end

And here is the spec:

      it 'Should redirect to the new page' do
        do_post
        response.should be_redirect
        response.should redirect_to( page_path( :action => 'show', :id
=> @static_page.permalink ) ) ## this one, with the page_path goes
fine
        response.should redirect_to( :action => 'show', :id =>
@static_page.permalink ) ## this one, without the page_path doesn't
work
      end

It seems that the redirect_to matcher doesn't recognize the current controller.

And here goes the full backtrace:

ActionController::MethodNotAllowed in 'StaticPagesController Handling
POST /pages With successful save Should redirect to the new page'
Only get, put, and delete requests are allowed.
/usr/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/routing.rb:1439:in
`recognize_path'
/home/mauricio/workspace/antares/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/redirect_to.rb:43:in
`path_hash'
/home/mauricio/workspace/antares/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/redirect_to.rb:38:in
`hash_from_url'
/home/mauricio/workspace/antares/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/redirect_to.rb:26:in
`actual_hash'
/home/mauricio/workspace/antares/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/redirect_to.rb:34:in
`actual_redirect_to_valid_route'
/home/mauricio/workspace/antares/vendor/plugins/rspec_on_rails/lib/spec/rails/matchers/redirect_to.rb:18:in
`matches?'
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/expectations/handler.rb:19:in
`handle_matcher'
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/expectations/extensions/object.rb:34:in
`should'
./spec/controllers/static_pages_controller_spec.rb:119:
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/example/example_methods.rb:78:in
`instance_eval'
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/example/example_methods.rb:78:in
`run_with_description_capturing'
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/example/example_methods.rb:19:in
`execute'
/usr/lib/ruby/1.8/timeout.rb:48:in `timeout'
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/example/example_methods.rb:16:in
`execute'
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:292:in
`execute_examples'
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:291:in
`each'
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:291:in
`execute_examples'
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/example/example_group_methods.rb:121:in
`run'
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:22:in
`run'
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:21:in
`each'
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/runner/example_group_runner.rb:21:in
`run'
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/runner/options.rb:90:in
`run_examples'
/home/mauricio/workspace/antares/vendor/plugins/rspec/lib/spec/runner/command_line.rb:19:in
`run'
script/spec:4:


On Fri, Feb 22, 2008 at 11:00 AM, David Chelimsky <dchelimsky at gmail.com> wrote:
> On Fri, Feb 22, 2008 at 7:50 AM, Maurício Linhares
>  <mauricio.linhares at gmail.com> wrote:
>  > Hello guys,
>  >
>  >  I have the following spec:
>  >
>  >     describe 'With successful save' do
>  >
>  >       it 'Should redirect to the new page' do
>  >         do_post
>  >         response.should be_redirect
>  >         response.should redirect_to( :action => 'show', :id =>
>  >  @static_page.permalink ) ## this is the line 118
>  >       end
>  >
>  >     end
>  >
>  >  And when I try to run it it gives me the following error:
>  >
>  >  1)
>  >  ActionController::MethodNotAllowed in 'StaticPagesController Handling
>  >  POST /pages With successful save Should redirect to the new page'
>  >  Only get, put, and delete requests are allowed.
>  >  ./spec/controllers/static_pages_controller_spec.rb:118:
>  >  script/spec:4:
>  >
>  >  So, the response is a redirect (the "response.should be_redirect"
>  >  passes) but when i try to be sure that it redirected to the right page
>  >  it throws this weird error to me. Any ideas about what I've done
>  >  wrong?
>
>  Can you post the full backtrace?
>
>  script/spec ./spec/controllers/static_pages_controller_spec.rb -b
>
>
>  >
>  >  I'm using RSpec 1.1.3 and Rails 2.0.2.
>  >
>  >  --
>  >  Maurício Linhares
>  >  http://alinhavado.wordpress.com/ (pt-br) |
>  >  http://codeshooter.wordpress.com/ (en)
>  >  João Pessoa, PB, +55 83 8867-7208
>  >  _______________________________________________
>  >  rspec-users mailing list
>  >  rspec-users at rubyforge.org
>  >  http://rubyforge.org/mailman/listinfo/rspec-users
>  >
>  _______________________________________________
>  rspec-users mailing list
>  rspec-users at rubyforge.org
>  http://rubyforge.org/mailman/listinfo/rspec-users
>



-- 
Maurício Linhares
http://alinhavado.wordpress.com/ (pt-br) |
http://codeshooter.wordpress.com/ (en)
João Pessoa, PB, +55 83 8867-7208


More information about the rspec-users mailing list