Rails 3.2 Engines-路由在测试用例中不起作用 [英] Rails 3.2 Engines - routes not working in test cases

查看:283
本文介绍了Rails 3.2 Engines-路由在测试用例中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 RailsAdmin 引擎(根据Wiki的说明)安装了我的应用程序

I mounted in my app the RailsAdmin engine ( according to the instructions from the wiki ) using

安装RailsAdmin :: Engine =>'/backend',:as =>'rails_admin'

mount RailsAdmin::Engine => '/backend', :as => 'rails_admin'

我不得不从引擎扩展一个控制器来添加一个before_filter. 在开发中运行我的应用程序时,我的扩展程序和其他引擎功能运行正常.

I had to extend one controller from the engine to add a before_filter. When running my app in development, my extension and the other engine features are working perfectly.

但是,在为我的before_filter使用Test :: Unit 编写功能测试时,我遇到了问题. 过滤器将在适当的时候启动并按预期工作,但是一旦由before过滤器调整"的操作重定向到索引(这是预期的结果),我将收到以下路由错误:

However I have an issue in writing functional tests using Test::Unit for my before_filter. The filter kicks in at the right moment and works as expected, but once the action "tweaked" by the before filter redirects to index ( which is an expected result ), I get the following routing error:

ActionController :: RoutingError:没有路由与{:controller =>"rails_admin/main"}匹配

ActionController::RoutingError: No route matches {:controller=>"rails_admin/main"}

导致异常的代码位于引擎的方法中,该方法用于重定向到索引或上一页(referer的种类);这是该方法的代码

The code leading to the exception lies in the engine's method to redirect to index or to the previous page ( kind of referer ); here is the code for the method

def back_or_index

def back_or_index

如果参数[:return_to].存在

if params[:return_to].presence

params [:return_to]

params[:return_to]

其他

index_path

index_path

结束

我发现,在开发环境中运行应用程序时,由index_path触发的url_for调用提供了正确的 path_segments(即::model_name =>'user'),从而使以下路由匹配

I discovered that when running the app in development, the url_for call triggered by index_path is supplied with the proper path_segments ( i.e.: :model_name => 'user' ) so that the route below is matched

   index GET|POST    /:model_name(.:format)                 rails_admin/main#index

另一方面,在运行测试时,未提供path_segments,因此在寻找匹配的路由时不满足:model_name 约束.

On the other hand, when running the tests, the path_segments are not supplied, thus the :model_name constraint is not satisfied when looking for a matching route.

由于我是引擎新手,所以有人可以告诉我我缺少什么吗?

Since I'm new to engines, can someone tell me what I'm missing?

在我看来,引擎似乎应该考虑安装生成index_path的事实,但是正如我说的那样,我不是专家....

To me it looks as if the engine should take into consideration the fact that is mounted generating the index_path, but as I said I'm not an expert in this....

推荐答案

use_route 是不够的: index_path 的调用还是失败了.

use_route was not enough: the call to index_path was failing anyway.

经过大量调试后,我仍然不确定为什么在 test development 中相同的代码无法正常工作的根本原因.

After lots of debugging, I am not yet completely sure about the root cause of why the same code was not working both in test and development.

我观察到的是开发中的 index_path 能够从请求的网址中推断 model_name ,而在中测试不是.

What I observed is that index_path in development is able to infer the model_name from the url of the request, while in test is is not.

尽管很丑陋,但我的解决方案是重写app/controllers/rails_admin/main_controller.rb中的以下方法

Although ugly, my solution was to override the method as follows in app/controllers/rails_admin/main_controller.rb

module RailsAdmin
 class MainController

        # Override to fix a routing error when running tests
    def back_or_index
      if params[:return_to].presence && params[:return_to].include?(request.host) && (params[:return_to] != request.fullpath)
        params[:return_to]
      else
        # forward :model_name as default_url_options is not aware of it in tests
        index_path( :model_name => params[:model_name] )
      end
    end

 end
end

在MainController的控制器规格中,我执行以下操作

In my controller specs for MainController I do the following

put :edit, { :model_name => 'user', :id => root.id, :user => { :role => :root }, :use_route => :rails_admin }

这就是我的路线

  devise_for :users
  mount RailsAdmin::Engine => '/backend', :as => 'rails_admin'
  mount Rich::Engine => '/rich', :as => 'rich'

如果有人有更好的解决方案,将不胜感激分享!

If anyone has a better solution, sharing would be really appreciated!

这篇关于Rails 3.2 Engines-路由在测试用例中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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