Dmitry S. wrote in post #1020600:
Can anybody hint my why does this happen? I’m only starting learning
about
RoR
Earlier it seemed to me that these ‘get/post’ methods of TestCase
do
respect routes.rb…
Am I missing something obvious? 
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:
- 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)>’
- 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’