【问题标题】:access path helper for an engine when testing parent Rails app with RSpec使用 RSpec 测试父 Rails 应用程序时访问引擎的路径助手
【发布时间】:2014-06-10 19:03:41
【问题描述】:

我有一个 Rails 引擎和一个包含它的 Rails 4 应用程序。我想在父 Rails 应用程序中编写一个 RSpec 测试,以验证 Rails 应用程序的控制器之一重定向到引擎内部的路径。当我为我的 Rails 应用程序运行 rake routes 时,我看到了我想要的引擎路径,并且它被命名了。我可以通过说my_engine.my_engine_path 在我的 Rails 应用程序的视图中引用该路径。但是,my_engine 在我的 Rails 应用程序的 RSpec 控制器测试中似乎并不存在。我不想在我的应用程序的spec_helper.rb 中执行以下操作,因为这似乎用我的引擎破坏了我的应用程序的路由:

config.include Module.new {
  def self.included base
    base.routes { MyEngine::Engine.routes }
  end
}, type: :controller

当我执行以下操作时,我得到undefined local variable or method 'my_engine'

expect(response).to redirect_to(my_engine.my_engine_path(some_route_param))

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-4 rspec


    【解决方案1】:

    我的问题出在我的路线上。我使用的是path 参数而不是id,因为控制器处理文件系统上的文件,而不是数据库记录。我有resources :my_controller 而不是resources :my_controller, param: :path, constraints: {path: /.*/}。因此,expect(response).to redirect_to(my_engine_path(some_route_param)) 在我的应用程序的 RSpec 控制器测试中运行良好,不需要 my_engine. 前缀。

    【讨论】:

      猜你喜欢
      • 2016-12-12
      • 1970-01-01
      • 2012-03-03
      • 2015-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多