[rspec-users] controller specs with non-RESTful routes

29 views
Skip to first unread message

Giuseppe Bertini

unread,
Sep 11, 2009, 4:28:22 PM9/11/09
to rspec...@rubyforge.org
Hello list,
this should be an easy one, but I seem to be stuck.
What is the proper way to mimic a POST request for a named, non-RESTful
route?

here's the route and the rest of the setup:

map.accept_invitation '/accept_invitation/:id', :controller =>
"invitations", :action => "accept", :method => "post"

class InvitationsController < ApplicationController
def accept
end
end

describe InvitationsController do
describe "POST 'accept'" do
it "should be successful" do
post :accept, :id => 1
response.should be_success
end
end
end

When I run this spec, I get:
No route matches {:controller=>"invitations", :action=>"accept",
:id=>"1"}

Can anyone please suggest what I should be writing instead to test this
request?

Thanks in advance!
Giuseppe
--
Posted via http://www.ruby-forum.com/.
_______________________________________________
rspec-users mailing list
rspec...@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Matt Wynne

unread,
Sep 14, 2009, 5:43:42 AM9/14/09
to rspec...@rubyforge.org

Looks about right to me. Must be something silly that you've missed,
but I'm afraid I can't see it.

cheers,
Matt

+447974 430184
ma...@mattwynne.net
http://mattwynne.net

David Chelimsky

unread,
Sep 14, 2009, 5:52:08 AM9/14/09
to rspec...@rubyforge.org
On Fri, Sep 11, 2009 at 3:28 PM, Giuseppe Bertini <li...@ruby-forum.com> wrote:
> Hello list,
> this should be an easy one, but I seem to be stuck.
> What is the proper way to mimic a POST request for a named, non-RESTful
> route?
>
> here's the route and the rest of the setup:
>
> map.accept_invitation '/accept_invitation/:id', :controller =>
> "invitations", :action => "accept", :method => "post"
>
> class InvitationsController < ApplicationController
>  def accept
>  end
> end
>
> describe InvitationsController do
>  describe "POST 'accept'" do
>    it "should be successful" do
>      post :accept, :id => 1
>      response.should be_success
>    end
>  end
> end
>
> When I run this spec, I get:
> No route matches {:controller=>"invitations", :action=>"accept",
> :id=>"1"}

I just fired up a brand new rails app and copied the above into
config/routes.rb, app/controllers/invitations_controller.rb, and
spec/controllers/invitations_controller_spec.rb, ran the spec and it
passed with flying color (green).

I'm using rails-2.3.4, ruby-1.8.7, rspec-1.2.8 and
rspec-rails-1.2.7.1. Which versions are you using?

Cheers,
David

Giuseppe Bertini

unread,
Sep 26, 2009, 2:56:14 AM9/26/09
to rspec...@rubyforge.org
Pretty please? :)

David Chelimsky

unread,
Sep 26, 2009, 5:15:12 AM9/26/09
to rspec...@rubyforge.org
On Sat, Sep 26, 2009 at 1:56 AM, Giuseppe Bertini <li...@ruby-forum.com> wrote:
> Pretty please? :)

Huh? Pretty please what? Matt and I have both responded. In my case I
asked you about the versions you have. You didn't quote any of the
previous posts in this thread, so I have no idea what you're asking
for at this point. Did you receive our responses?

Giuseppe Bertini

unread,
Sep 26, 2009, 9:10:48 AM9/26/09
to rspec...@rubyforge.org
> Huh? Pretty please what? Matt and I have both responded. In my case I
> asked you about the versions you have. You didn't quote any of the
> previous posts in this thread, so I have no idea what you're asking
> for at this point. Did you receive our responses?

So sorry, I must be missing something.
I am looking right now at this page:

http://www.ruby-forum.com/topic/195172

And what I see is my original post, my own "pretty please" and your
"Huh?", period.

Looking at the index page (http://www.ruby-forum.com/forum/32) does not
help.

I'm wondering--am I looking at the wrong forum page? But then, why do I
see your latest response (and received the email notification I
requested), but not the previous?

Sorry for the trouble, I would appreciate if you could point me to the
proper URL, if this is the problem.

Thanks a bunch,
Giuseppe

David Chelimsky

unread,
Sep 26, 2009, 10:28:12 AM9/26/09
to rspec...@rubyforge.org
On Sat, Sep 26, 2009 at 8:10 AM, Giuseppe Bertini <li...@ruby-forum.com> wrote:
>> Huh? Pretty please what? Matt and I have both responded. In my case I
>> asked you about the versions you have. You didn't quote any of the
>> previous posts in this thread, so I have no idea what you're asking
>> for at this point. Did you receive our responses?
>
> So sorry, I must be missing something.
> I am looking right now at this page:
>
> http://www.ruby-forum.com/topic/195172

Well - that _should_ be one of two correct URLs. The forum is mirrored
w/ a google group. I see there are posts missing from the forum, but I
see all the posts in the google group:

http://groups.google.com/group/rspec/browse_thread/thread/8c079724053b5ce7

We don't offer much help, as Matt doesn't see a problem and I can't
reproduce it - but at least we tried to play :)

Stephen Eley

unread,
Sep 26, 2009, 10:53:09 AM9/26/09
to rspec-users
On Sat, Sep 26, 2009 at 10:28 AM, David Chelimsky <dchel...@gmail.com> wrote:
>
> We don't offer much help, as Matt doesn't see a problem and I can't
> reproduce it - but at least we tried to play :)

I'll take a turn! Helps me procrastinate on my own stuff. >8->

Giuseppe: have you tried running 'rake routes' to see what the
application thinks you valid routes are? Reconciling that with what's
going on in routes.rb is a good next step.

Also, when testing behavior has me stumped, I often remember far too
late to open up my Web browser and poke at the pages directly, as a
human being. I'll often see error messages that way that RSpec
doesn't think to show me, or I can view source and see what the links
in my form _really_ are.

Those aren't direct answers (I'm with Matt, I don't see anything wrong
here unless something else in routes.rb is conflicting with the line
you posted) but they've been useful tactics for me in the past.


--
Have Fun,
  Steve Eley (sfe...@gmail.com)
  ESCAPE POD - The Science Fiction Podcast Magazine
  http://www.escapepod.org

giuseb

unread,
Sep 26, 2009, 10:44:29 AM9/26/09
to rspec...@rubyforge.org
> > So sorry, I must be missing something.
> > I am looking right now at this page:
>
> >http://www.ruby-forum.com/topic/195172
>
> Well - that _should_ be one of two correct URLs. The forum is mirrored
> w/ a google group. I see there are posts missing from the forum, but I
> see all the posts in the google group

Yes, now I see, thanks.
So, back to the original issue, I have rails 2.3.4, rspec 1.2.8, rspec-
rails 1.2.7.1, ruby 1.8.6 (universal darwin9.0)

I too have created a new app with only the above-mentioned code added
to the rails-generated and rspec-generated stuff. I am still getting
the "No route matches..." error

I have posted the app here:
git://github.com/giuseb/spec_named_route.git

I am somewhat nervous that this is going to turn out a silly mistake
on my part as Matt suggested, but still...

Giuseppe

David Chelimsky

unread,
Sep 26, 2009, 11:00:21 AM9/26/09
to rspec-users
On Sat, Sep 26, 2009 at 9:44 AM, giuseb <giuseppe...@gmail.com> wrote:
>> > So sorry, I must be missing something.
>> > I am looking right now at this page:
>>
>> >http://www.ruby-forum.com/topic/195172
>>
>> Well - that _should_ be one of two correct URLs. The forum is mirrored
>> w/ a google group. I see there are posts missing from the forum, but I
>> see all the posts in the google group
>
> Yes, now I see, thanks.
> So, back to the original issue, I have rails 2.3.4, rspec 1.2.8, rspec-
> rails 1.2.7.1, ruby 1.8.6 (universal darwin9.0)
>
> I too have created a new app with only the above-mentioned code added
> to the rails-generated and rspec-generated stuff. I am still getting
> the "No route matches..." error
>
> I have posted the app here:
> git://github.com/giuseb/spec_named_route.git
>
> I am somewhat nervous that this is going to turn out a silly mistake
> on my part as Matt suggested, but still...
>
> Giuseppe

Thanks for posting the app - that was very helpful.

I've got good news and bad news: The good news is that I hadn't set up
my routes as cleanly as yours, and when I did I was able to reproduce
the error.

The bad news is that this is a deficiency the underlying Rails testing
framework that rspec-rails wraps. I tried the same example in a rails
functional test and got the same failure:

class InvitationsControllerTest < ActionController::TestCase

test "the post accept invitation should be successful" do
get :accept, :id => "1"
assert_response :success
end
end


test_the_post_accept_invitation_should_be_successful(InvitationsControllerTest):
ActionController::RoutingError: No route matches {:action=>"accept",
:controller=>"invitations", :id=>"1"}
/test/functional/invitations_controller_test.rb:6:in
`test_the_post_accept_invitation_should_be_successful'

You wanna raise a ticket in the rails lighthouse?

giuseb

unread,
Sep 26, 2009, 12:49:02 PM9/26/09
to rspec...@rubyforge.org
> The bad news is that this is a deficiency the underlying Rails testing
> framework that rspec-rails wraps. I tried the same example in a rails
> functional test and got the same failure:
>
> class InvitationsControllerTest < ActionController::TestCase
>
>   test "the post accept invitation should be successful" do
>     get :accept, :id => "1"
>     assert_response :success
>   end
> end
>
> test_the_post_accept_invitation_should_be_successful(InvitationsControllerTest):
> ActionController::RoutingError: No route matches {:action=>"accept",
> :controller=>"invitations", :id=>"1"}
>     /test/functional/invitations_controller_test.rb:6:in
> `test_the_post_accept_invitation_should_be_successful'
>
> You wanna raise a ticket in the rails lighthouse?

I just did--my very first ticket!!

Two more things:
- I noticed that you tested a get request, while i was doing post.
Tried them both again, same outcome.
- what did your "unclean" routes look like that did NOT cause the
problem?

Thanks,
Giuseppe

David Chelimsky

unread,
Sep 26, 2009, 1:41:40 PM9/26/09
to rspec-users
On Sat, Sep 26, 2009 at 11:49 AM, giuseb <giuseppe...@gmail.com> wrote:
>> The bad news is that this is a deficiency the underlying Rails testing
>> framework that rspec-rails wraps. I tried the same example in a rails
>> functional test and got the same failure:
>>
>> class InvitationsControllerTest < ActionController::TestCase
>>
>>   test "the post accept invitation should be successful" do
>>     get :accept, :id => "1"
>>     assert_response :success
>>   end
>> end
>>
>> test_the_post_accept_invitation_should_be_successful(InvitationsControllerTest):
>> ActionController::RoutingError: No route matches {:action=>"accept",
>> :controller=>"invitations", :id=>"1"}
>>     /test/functional/invitations_controller_test.rb:6:in
>> `test_the_post_accept_invitation_should_be_successful'
>>
>> You wanna raise a ticket in the rails lighthouse?
>
> I just did--my very first ticket!!
>
> Two more things:
> - I noticed that you tested a get request, while i was doing post.

Oh yeah - I had tried both and copied the wrong one in the email :)

> Tried them both again, same outcome.
> - what did your "unclean" routes look like that did NOT cause the
> problem?

Just the defaults:

map.connect ':controller/:action/:id'

HTH

giuseb

unread,
Sep 26, 2009, 6:03:58 PM9/26/09
to rspec...@rubyforge.org
Steve,

> Giuseppe: have you tried running 'rake routes' to see what the
> application thinks you valid routes are?  Reconciling that with what's
> going on in routes.rb is a good next step.

The following line is the whole of the 'rake routes' output:

accept_invitation /accept_invitation/:id
{:action=>"accept", :method=>"get", :controller=>"invitations"}

> Also, when testing behavior has me stumped, I often remember far too
> late to open up my Web browser and poke at the pages directly, as a
> human being.

I did visit:
http://localhost:3000/accept_invitation/1
and the request gets properly routed by the app.
As David pointed out earlier today, this behavior appears to be
related to rails' testing framework, rather than to rspec.

I have raised a ticket in the rails lighthouse:
https://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/3263-failing-functional-test-of-a-named-route

Cheers,
Giuseppe

Reply all
Reply to author
Forward
0 new messages