本番環境でベース URL のサブディレクトリが不足している Rails プロジェクトがあり、dev と prod をできるだけ近づけるために、dev でそのように動作させたいと考えています。次のようにルートファイルを設定しています。
Foo::Application.routes.draw do
def draw_routes
root :to=>'foo#home'
resources :cats, :only=>[:create] do
end
if Rails.env.production?
scope :protocol=>'http://' do
draw_routes
end
else
scope :path=>"/foo", :protocol=>'http://' do
draw_routes
end
end
end
私の CatsController は次のようなものです:
class CatsController < ApplicationController
def create
@cat = Cat.new(:name=>"Bob")
if @cat.save()
redirect_to root
end
end
end
Create Cat メソッドをテストしたいので、spec/controllers/cats_controller_spec.rb で rspec テストをセットアップします。
require 'spec_helper'
describe CatsController do
describe "calling create cat with good data" do
it "should add a cat" do
expect {post(:action=>:create)}.to change(Cat, :count).by(1)
end
end
end
ただし、テストを実行すると、
Failure/Error: post :create
ActionController::RoutingError:
No route matches {:controller=>"cats", :action=>"create"}
# ./spec/controllers/cats_controller_spec.rb:5:in `(root)'
Rake routes は、私のルートがそこにあると言っています! ここで何が起こっているのですか?なぜこれが失敗するのですか?
Rake Routes:
cats POST /foo/cats(.:format) cats#create {:protocol=>"http://"}