2011-02-03 24 views
2

我在rspec的文件有问题edit_user_path(user)的Rails 3 Rspec的2问题:语言环境和REST类型的路径

it "should forward to the requested page after signin" do 
    user = Factory(:user) 
    visit edit_user_path(user) 

    fill_in :email, :with => user.email 
    fill_in :password, :with => user.password 
    click_button 

    response.should render_template('users/edit') 
end 

我得到这个错误:

Failure/Error: visit edit_user_path(user) 
ActionController::RoutingError: 
No route matches {:action=>"edit", :controller=>"users", :locale=>#<User id: 1, name: "Max Mustermann", email: "[email protected]", encrypted_password: "1b0befc5caf12c2abf53d99sdfsd5ba15ebe28362eae21b2fb3...", salt: "331ca07444f5dcee76605bsdfasd95c833b7d5135b36a253723...", created_at: "2011-02-03 18:54:34", updated_at: "2011-02-03 18:54:34">} 

我不知道为什么使用user作为:locale以及为什么没有:iduser.id。我使用:locale来设置语言,例如localhost:3000/en/home或localhost:3000/de/home。

这是我routs.rb

Hoapp::Application.routes.draw do 

scope '(:locale)', :locale => /de|en/ do 

    resources :users 
    resources :sessions, :only => [:new, :create, :destroy] 
    resources :hotels, :only => [:create, :destroy] 

    root      :to => 'pages#home' 
    match '/signup',   :to => 'users#new' 
    match '/signin',   :to => 'sessions#new' 
    match '/signout',   :to => 'sessions#destroy' 
    match '/about',   :to => 'pages#about' 
    match '/blog',   :to => 'pages#blog' 
    match '/contact',   :to => 'pages#contact' 
    match '/help',   :to => 'pages#help' 
    match '/overview',  :to => 'pages#overview' 
    match '/preview',   :to => 'pages#preview' 
    match '/settings',  :to => 'pages#settings' 

    match 'm',    :to => 'mobile#home' 
    match 'apartments',  :to => 'mobile#apartments' 
    match 'apartment_info', :to => 'mobile#apartment_info' 
    match 'apartment_images', :to => 'mobile#apartment_images' 
    match 'apartment_price', :to => 'mobile#apartment_price' 
    match 'wellness',   :to => 'mobile#wellness' 
    match 'test',    :to => 'mobile#test' 

    scope "m" do 
    resources :requests, :only => [:new, :create] 
    match 'request_sent', :to => 'requests#sent' 
    end 

    match ':vanity_url/', :to => "mobile#home" 
end 
+0

你能后的`routes.rb`文件的相关部分? – zetetic 2011-02-08 02:22:42

+0

我已经添加了我的routes.rb。 – 2011-02-24 19:06:08

回答

3

当您使用的语言环境也许url_for无法推断正确的参数?试着这样说:

visit edit_user_path(:id=>user.id) 
4

好,我找到了一种方法在测试中解决这个问题(参见https://github.com/rspec/rspec-rails/issues/255

我把这个代码在spec_helper.rb

class ActionView::TestCase 
    class TestController 
    def default_url_options 
     {:locale => 'en'} 
    end 
    end 
end 
2

包括这spec_helper.rb工作对我来说:

config.before do 
    default_url_options[:locale] = I18n.default_locale 
end 
0

我试图把这个config/environments/test.rb的d为我工作:

config.action_controller.default_url_options = { locale: I18n.locale }