2013-03-15 66 views
1

使用Rails 4.0.0beta1,我試圖創建一些集成測試。 所有我的網址是局部的和我locale(如/en/user/new),我每次我打電話new_user_url具有以下錯誤:如何在集成測試的默認url選項中設置區域設置

ActionController::UrlGenerationError: No route matches {:action=>"new", :controller=>"user"} missing required keys: [:locale] 

我試過在following question通過@Balint ERDI給出的解決方案

class ActionController::Integration::Session 
    def url_for_with_default_locale(options) 
    options = { locale: I18n.locale }.merge(options) 
    url_for_without_default_locale(options) 
    end 

    alias_method_chain :url_for, :default_locale 
end 

它的工作原理,但給我,因爲rails4的棄用警告:

DEPRECATION WARNING: ActionController::Integration is deprecated and will be removed, use ActionDispatch::Integration instead. (called from <top (required)> at /path/to/project/test/test_helper.rb:46) 
DEPRECATION WARNING: ActionController::IntegrationTest is deprecated and will be removed, use ActionDispatch::IntegrationTest instead. (called from <top (required)> at /path/to/project/test/test_helper.rb:46) 

對我的控制器測試中,我添加了這個:

class ActionController::TestCase 

    module Behavior 
    def process_with_default_locale(action, http_method = 'GET', parameters = nil, session = nil, flash = nil) 
     parameters = { locale: I18n.locale }.merge(parameters || {}) 
     process_without_default_locale(action, http_method, parameters, session, flash) 
    end 

    alias_method_chain :process, :default_locale 
    end 
end 

我還測試了直接添加default_url_options方法進入測試,但沒有奏效。

如何在集成測試中設置默認的url參數?

回答

1

好吧,看起來好像用ActionDispatch代替ActionController一樣簡單。我不知道爲什麼它沒有工作過,但因爲我更新爲棄用rake test:integrationrails test integration似乎工作的最新軌道:

class ActionDispatch::Integration::Session 
    def url_for_with_default_locale(options) 
    options = { locale: I18n.locale }.merge(options) 
    url_for_without_default_locale(options) 
    end 

    alias_method_chain :url_for, :default_locale 
end 
2

爲我工作(至少在Rails的4.2的選項。 0)是在我的test/test_helper.rb中添加設置方法到ActionDispatch::IntegrationTest類:

class ActionDispatch::IntegrationTest 
    def setup 
    self.default_url_options = { locale: I18n.default_locale } 
    end 
end