简体   繁体   English

使用RSpec测试父Rails应用程序时引擎的访问路径帮助器

[英]access path helper for an engine when testing parent Rails app with RSpec

I have a Rails engine and a Rails 4 app that contains it. 我有一个Rails引擎和一个包含它的Rails 4应用程序。 I want to write an RSpec test in the parent Rails app that verifies one of the Rails app's controllers redirects to a path inside the engine. 我想在父级Rails应用程序中编写RSpec测试,以验证Rails应用程序的控制器之一重定向到引擎内部的路径。 When I run rake routes for my Rails app, I see the engine path I want, and it's named. 当我为Rails应用程序运行rake routes ,我会看到所需的引擎路径,并对其进行了命名。 I can refer to that path inside a view in my Rails app by saying my_engine.my_engine_path . 我可以通过说my_engine.my_engine_path在我的Rails应用程序的视图内引用该路径。 However, my_engine doesn't seem to be a thing inside my Rails app's RSpec controller test. 但是,在我的Rails应用程序的RSpec控制器测试中, my_engine似乎不是问题。 I don't want to do the following in my app's spec_helper.rb because that seems to clobber my app's routes with my engine's: 我不想在我的应用程序的spec_helper.rb执行以下操作,因为这似乎spec_helper.rb我的应用程序的路由与引擎的连接spec_helper.rb

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

When I do the following, I get undefined local variable or method 'my_engine' : 当我执行以下操作时,会得到undefined local variable or method 'my_engine'

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

My problem was in my routes. 我的问题出在我的路线上。 I was using a path parameter instead of id because the controller deals with files on the file system, not database records. 我使用的是path参数而不是id因为控制器处理文件系统上的文件,而不是数据库记录。 I had resources :my_controller instead of resources :my_controller, param: :path, constraints: {path: /.*/} . 我有resources :my_controller而不是resources :my_controller, param: :path, constraints: {path: /.*/} So doing expect(response).to redirect_to(my_engine_path(some_route_param)) works fine in my app's RSpec controller test, no my_engine. 所以在我的应用程序的RSpec控制器测试中没有对my_engine. expect(response).to redirect_to(my_engine_path(some_route_param)) my_engine. prefix necessary. 需要前缀。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM