i18n重定向打破了我的测试

时间:2010-04-06 14:49:31

标签: ruby-on-rails rspec internationalization rails-routing

我有一个很大的应用程序,通过rspec进行了一千多次测试。

我们只是选择重定向任何页面,如:

/
/foo
/foo/4/bar/34
...

TO:

/en
/en/foo
/fr/foo/4/bar/34 
....

所以我在application.rb中创建了一个before过滤器,如下所示:

if params[:locale].blank?
  headers["Status"] = "301 Moved Permanently"
  redirect_to request.env['REQUEST_URI'].sub!(%r(^(http.?://[^/]*)?(.*))) { "#{$1}/#{I18n.locale}#{$2}" }
end

它工作得很好但是...它打破了很多我的测试,例如:

  it "should return 404" do
    Video.should_receive(:failed_encodings).and_return([])
    get :last_failed_encoding
    response.status.should == "404 Not Found"
  end

要修复此测试,我应该这样做:

    get :last_failed_encoding, :locale => "en"

但是......严肃地说,我不想一个一个地修复我的所有考试......

我尝试将语言环境设为默认参数,如下所示:

class ActionController::TestCase
  alias_method(:old_get, :get) unless method_defined?(:old_get)
  def get(path, parameters = {}, headers = nil)
    parameters.merge({:locale => "fr"}) if parameters[:locale].blank?
    old_get(path, parameters, headers)
  end  
end

...但无法使这项工作...... 有什么想法??

1 个答案:

答案 0 :(得分:0)

为什么不在之前定义此语言环境?

在你的applicationController中:

params[:locale] = i18n.locale if params[:locale].blank?

申请后,本地定义和未来链接可能会很好。

相关问题