【问题标题】:RSpec feature specs can't find routes for Rails EngineRSpec 功能规范找不到 Rails 引擎的路线
【发布时间】:2017-06-15 15:05:15
【问题描述】:

我正在使用 rails v5.1.0rspec-rails 3.5.2 开发 Rails 引擎。

我有一个简单的功能规范:

require "rails_helper"

module MyEngineName
  RSpec.feature "Some Feature", type: :feature do
    it "user can navigate to page and blah blah", :js do
      visit edit_job_path(1)
      # .... other stuff
    end
  end
end

这会引发错误

undefined method `edit_job_path' for #<RSpec::ExampleGroups::SomeFeature:0x007fc098a570e8>

因为找不到路由助手edit_job_path

为了让我的功能规范访问我的引擎路线,我需要做些什么特别的事情吗?

RSpec 文档mentions that you can specify the engine routes,但这似乎只适用于路由规范。当我将它添加到功能规范中时,它失败了undefined method 'routes'

谢谢!

编辑: 因为我的路线文件被请求,所以在此处添加。很短——

# config/routes.rb
MyEngineName::Engine.routes.draw do
  root to: redirect("/my_engine_name/jobs")
  resources :jobs
end

rake 的所有路由列表

> rake app:routes
   ....
   ....

Routes for MyEngineName::Engine:
    root GET    /                        redirect(301, /my_engine_name/jobs)
    jobs GET    /jobs(.:format)          my_engine_name/jobs#index
         POST   /jobs(.:format)          my_engine_name/jobs#create
 new_job GET    /jobs/new(.:format)      my_engine_name/jobs#new
edit_job GET    /jobs/:id/edit(.:format) my_engine_name/jobs#edit
     job GET    /jobs/:id(.:format)      my_engine_name/jobs#show
         PATCH  /jobs/:id(.:format)      my_engine_name/jobs#update
         PUT    /jobs/:id(.:format)      my_engine_name/jobs#update
         DELETE /jobs/:id(.:format)      my_engine_name/jobs#destroy

【问题讨论】:

  • 显示您的routes.rb 文件,至少前几行。
  • @chumakoff - 用它编辑了帖子。谢谢!
  • @chumakoff,你有没有搞定它?

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


【解决方案1】:

对于 API 请求规范,我尝试了两种方法,但都不起作用

routes { MyEngine::Engine.routes }

RSpec.configure do |config|
  config.before :each, type: :request do
    helper.class.include MyEngine::Engine.routes.url_helpers
  end
end

如果上述解决方案不起作用尝试将帮助程序网址加载为 rspec 配置,如下所示:

RSpec.configure do |config|
  config.include MyEngine::Engine.routes.url_helpers
end

【讨论】:

    【解决方案2】:

    这应该会有所帮助。确保你有MyEngineName::Engine.routes,而不是MyEngineName.routes

    require "rails_helper"
    
    module MyEngineName
      RSpec.feature "Some Feature", type: :feature do
        routes { MyEngineName::Engine.routes }
    
        it "user can navigate to page and blah blah", :js do
        # ...
    

    或(另一种解决方案)

    # place this in `spec/rails_helper.rb` file
    RSpec.configure do |config|
      config.before :each, type: :feature do
        helper.class.include MyEngineName::Engine.routes.url_helpers
      end
    end
    

    【讨论】:

    • 谢谢!第一种方法会引发错误 - undefined method 'routes' for RSpec::ExampleGroups::SomeFeature:Class (NoMethodError) - 因为我认为 routes 不是有效的方法定义(不知道为什么?)。第二个显示了一些希望,因为它肯定能够识别edit_job_path!但它在尝试加载该页面的资产时出错 - No route matches [GET] "/assets/my_engine_name/application-(fingerprint).js"kinda 是因为该路由不是由路由助手定义的,对吗?我也不知道为什么它会在test 环境中对资产进行指纹识别...谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 2011-07-05
    • 1970-01-01
    • 2013-06-26
    • 1970-01-01
    • 2012-07-16
    相关资源
    最近更新 更多