i18n 重定向破坏了我的测试

发布于 2024-08-28 01:09:30 字数 1165 浏览 3 评论 0原文

我有一个大型应用程序,通过 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

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

I have a big application covered by more than a thousand tests via rspec.

We just made the choice to redirect any page like :

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

TO :

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

So I made a before filter in application.rb like so :

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

It's working great but ... It's breaking a lot of my tests, ex :

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

To fix this test, I should do :

    get :last_failed_encoding, :locale => "en"

But ... seriously I don't want to fix all my test one by one ...

I tried to make the locale a default parameter like this :

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

... but couldnt make this work ...
Any idea ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

神魇的王 2024-09-04 01:09:30

如果之前没有定义这个区域设置,为什么不定义它呢?

在您的 applicationController 中:

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

在您的应用程序具有本地定义和未来链接之后可能会很好。

Why don't define this locale if not before ?

In your applicationController :

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

After you application has the local define and futur link could be good.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文