私はそのようなコントローラーを持っています:
class StatsController < ApplicationController
def users_in_system
@users = User.all
end
end
ルート:
scope ":locale", locale: /#{I18n.available_locales.join("|")}/ do
devise_for :users, :skip => [:registrations]
as :user do
get 'users/edit' => 'devise/registrations#edit', :as => 'edit_user_registration'
put 'users' => 'devise/registrations#update', :as => 'user_registration'
get '/users/sign_out' => 'devise/sessions#destroy'
end
match '/users_list', :to => 'stats#users_in_system'
match '/about', :to => 'pages#about'
root :to => 'pages#home'
end
match '*path', to: redirect("/#{I18n.default_locale}/%{path}"), constraints: lambda { |req| !req.path.starts_with? "/#{I18n.default_locale}/" }
match '', to: redirect("/#{I18n.default_locale}")
Rake routes
:
new_user_session GET /:locale/users/sign_in(.:format) devise/sessions#new {:locale=>/en|ru/}
user_session POST /:locale/users/sign_in(.:format) devise/sessions#create {:locale=>/en|ru/}
destroy_user_session DELETE /:locale/users/sign_out(.:format) devise/sessions#destroy {:locale=>/en|ru/}
user_password POST /:locale/users/password(.:format) devise/passwords#create {:locale=>/en|ru/}
new_user_password GET /:locale/users/password/new(.:format) devise/passwords#new {:locale=>/en|ru/}
edit_user_password GET /:locale/users/password/edit(.:format) devise/passwords#edit {:locale=>/en|ru/}
PUT /:locale/users/password(.:format) devise/passwords#update {:locale=>/en|ru/}
edit_user_registration GET /:locale/users/edit(.:format) devise/registrations#edit {:locale=>/en|ru/}
user_registration PUT /:locale/users(.:format) devise/registrations#update {:locale=>/en|ru/}
users_sign_out GET /:locale/users/sign_out(.:format) devise/sessions#destroy {:locale=>/en|ru/}
users_list /:locale/users_list(.:format) stats#users_in_system {:locale=>/en|ru/}
about /:locale/about(.:format) pages#about {:locale=>/en|ru/}
root /:locale(.:format) pages#home {:locale=>/en|ru/}
/*path(.:format) :controller#:action
/ :controller#:action
そして、コントローラーのテストを書きたいです。だから私はそのスペックを試してみます:
require 'spec_helper'
describe StatsController do
render_views
before(:each){ @user = FactoryGirl.create( :user ) }
describe "GET 'users_in_system'" do
describe "for non-signed users" do
it "should deny access" do
get :users_in_system
#...
end
end
end
end
そしてそれは失敗します:
1) StatsController GET 'users_in_system' for non-signed users should deny access
Failure/Error: get :users_in_system
ActionController::RoutingError:
No route matches {:controller=>"stats", :action=>"users_in_system"}
# ./spec/controllers/stats_controller_spec.rb:11:in `block (4 levels) in <top (required)>'
ブラウザでこのページが開き、すべてが機能します。rspecはどのようにしてそのようなエラーを生成できますか?!
UPD1:おそらく、ルートへのi18n統合は私のテストを破ると思います。
UPD2:
私のapplication.rb:
require File.expand_path('../boot', __FILE__)
require 'rails/all'
if defined?(Bundler)
# If you precompile assets before deploying to production, use this line
Bundler.require(*Rails.groups(:assets => %w(development test)))
# If you want your assets lazily compiled in production, use this line
# Bundler.require(:default, :assets, Rails.env)
end
module MyApp
class Application < Rails::Application
config.i18n.default_locale = :ru
config.encoding = "utf-8"
config.filter_parameters += [:password]
config.active_support.escape_html_entities_in_json = true
config.active_record.whitelist_attributes = true
config.assets.enabled = true
config.assets.version = '1.0'
end
end