Method get of ActionController::TestCase ignores routes.rb?

Hi!

I’m running into issue which seems to indicate that
ActionController::TestCase.get() method ignores what I have in
routes.rb.
Happens in 3.0.10 and 3.1.0 too.

I have the following RSpec2 test of my XmlRpcController#index action:

it “should get nothing in response to GET request” do
get :index
response.response_code.should == 400 #bad_request
end

And the only line related to this route in routes.rb is:

post ‘rpc’, :to => “xml_rpc#index”

‘rake routes’ also shows only this route defined.

As a result when I run this test that action actually DOES get executed!
I
judge this by putting a simple puts inside it) and also a log contains:

“Processing by XmlRpcController#index as HTML”

Also if I go to ‘localhost:3000/rpc’ in browser - it says “no route
found”:
just like it should. But tests have other behavior and this puzzles
me…

Can anybody hint my why does this happen? I’m only starting learning
about
RoR :slight_smile: Earlier it seemed to me that these ‘get/post’ methods of TestCase
do
respect routes.rb…

Am I missing something obvious? :slight_smile:

Dmitry S. wrote in post #1020600:

Can anybody hint my why does this happen? I’m only starting learning
about
RoR :slight_smile: Earlier it seemed to me that these ‘get/post’ methods of TestCase
do
respect routes.rb…

Am I missing something obvious? :slight_smile:

With this controller:

class PagesController < ApplicationController
def index
end

def home
end

def contact
end

end

…and this single route:

T2App::Application.routes.draw do
root :to => “pages#index”
end

…and these tests:

require ‘spec_helper’

describe PagesController do

describe “POST ‘contact’” do
it “should be successful” do
post ‘contact’
response.should be_success
end
end

describe “GET ‘contact’” do
it “should be successful” do
get ‘contact’
response.should be_success
end
end

end

I get these results:

$ bundle exec rspec spec/
No DRb server is running. Running in local process instead …
FF

Failures:

  1. PagesController POST ‘contact’ should be successful
    Failure/Error: post ‘contact’
    ActionController::RoutingError:
    No route matches {:controller=>“pages”, :action=>“contact”}

    ./spec/controllers/pages_controller_spec.rb:6:in `block (3

levels) in <top (required)>’

  1. PagesController GET ‘contact’ should be successful
    Failure/Error: get ‘contact’
    ActionController::RoutingError:
    No route matches {:controller=>“pages”, :action=>“contact”}

    ./spec/controllers/pages_controller_spec.rb:21:in `block (3

levels) in <top (required)>’

Finished in 0.09563 seconds
2 examples, 2 failures

Failed examples:

rspec ./spec/controllers/pages_controller_spec.rb:5 # PagesController
POST ‘contact’ should be successful
rspec ./spec/controllers/pages_controller_spec.rb:20 # PagesController
GET ‘contact’ should be successful

If I add a ‘post’ route to routes.db:

T2App::Application.routes.draw do
root :to => “pages#index”

post “pages/contact”

end

I get these results:

$ bundle exec rspec spec/
No DRb server is running. Running in local process instead …

Finished in 0.11245 seconds
2 examples, 0 failures

If I change the ‘post’ route to a ‘get’ route:

T2App::Application.routes.draw do
root :to => “pages#index”

get “pages/contact”
end

I get these results:

$ bundle exec rspec spec/
No DRb server is running. Running in local process instead …

Finished in 0.11352 seconds
2 examples, 0 failures

So to me it looks like if you define either a get route or a post route
for an action,
then rspec allows both get and post requests. I don’t know if that is
by design, or if that is a bug. I have the following in my gemfile for
both the development and test environment:

gem ‘rspec-rails’, ‘2.6.1’

Interestingly, the following tests do work as expected:

describe “GET ‘contact’” do
it “should be successful” do
{ :get => ‘pages/contact’ }.should be_routable
end
end

describe “POST ‘contact’” do
it “should be successful” do
{ :post => ‘pages/contact’ }.should be_routable
end
end

For instance, with this route:

T2App::Application.routes.draw do
get “pages/contact”
end

I get these results:

$ bundle exec rspec spec/
No DRb server is running. Running in local process instead …
…F

Failures:

  1. PagesController POST ‘contact’ should be successful
    Failure/Error: { :post => ‘pages/contact’ }.should be_routable
    expected {:post=>“pages/contact”} to be routable

    ./spec/controllers/pages_controller_spec.rb:28:in `block (3

levels) in <top (required)>’

Finished in 0.12608 seconds
3 examples, 1 failure

Failed examples:

rspec ./spec/controllers/pages_controller_spec.rb:27 # PagesController
POST ‘contact’ should be successful

Wow, that matcher works indeed!
Thank you very much for analysis and for a solution!