如何为功能测试设置语言环境 default_url_options (Rails) [英] How to set locale default_url_options for functional tests (Rails)

查看:20
本文介绍了如何为功能测试设置语言环境 default_url_options (Rails)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 application_controller 中,我设置了以下设置以包含 url_for 生成的所有路径的语言环境:

In my application_controller, I have the following set to include the locale with all paths generated by url_for:

  def default_url_options(options={})
    { :locale => I18n.locale }
  end

然后我的资源路由有一个 :path_prefix = "/:locale"

My resource routes then have a :path_prefix = "/:locale"

在网站上运行良好.

但是当涉及到我的功能测试时,:locale 没有与生成的 url 一起传递,因此它们都失败了.我可以通过在我的测试中将语言环境添加到 url 来解决它,如下所示:

But when it comes to my functional tests, the :locale is not passed with the generated urls, and therefore they all fail. I can get around it by adding the locale to the url in my tests, like so:

  get :new, :locale => 'en'

但我不想手动将语言环境添加到每个功能测试中.

But I don't want to have to manually add the locale to every functional test.

我尝试将上面的default_url_options def添加到test_helper,但似乎没有效果.

I tried adding the default_url_options def above to test_helper, but it seems to have no effect.

有什么方法可以更改 default_url_options 以包含所有测试的语言环境?

Is there any way I can change the default_url_options to include the locale for all my tests?

谢谢.

推荐答案

查看控制器测试用例如何生成 url 似乎没有直接的方法让它使用 defualt_url_options.实际创建 url 的主要块(在测试中)看起来像这样(http://github.com/rails/rails/blob/master/actionpack/lib/action_controller/test_case.rb):

Looking through how the controller test case generates the url there doesn't seem to be a direct way to have it use the defualt_url_options. The main block that actually does the url creationg (in the tests) looks like this (http://github.com/rails/rails/blob/master/actionpack/lib/action_controller/test_case.rb):

private
  def build_request_uri(action, parameters)
    unless @request.env['REQUEST_URI']
      options = @controller.__send__(:rewrite_options, parameters)
      options.update(:only_path => true, :action => action)

      url = ActionController::UrlRewriter.new(@request, parameters)
      @request.request_uri = url.rewrite(options)
    end
  end

这由流程方法调用,而流程方法又由 get、post、head 或 put 方法调用.获得您正在寻找的东西的一种方法可能是 alias_chain 过程方法.

This gets called by the process method which is in turn called by the get, post, head, or put methods. One way to maybe get what you are looking for might be to alias_chain the process method.

class ActionController::TestCase
  def process_with_default_locale(action, parameters = nil, session = nil, flash = nil, http_method = 'GET')
    parameters = {:locale=>'en'}.merge(parameters||{})
    process_without_default_locale(action, parameters, session, flash, http_method)
  end
  alias_method_chain :process, :default_locale
end

你会想把它放到你的测试助手中,我认为是在 TestCase 类之外.让我知道它是如何为你工作的,我还没有真正测试过,所以我们拭目以待.

You'll want to put that into your test helper, outside of the TestCase class I think. Let me know how it works for you, I haven't really tested it out so we'll see.

这篇关于如何为功能测试设置语言环境 default_url_options (Rails)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆