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

查看:92
本文介绍了如何为功能测试设置语言环境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的直接方法.实际执行网址创建的主要块(在测试中)如下所示( 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

这由process方法调用,而该方法又由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天全站免登陆