gpt4 book ai didi

ruby-on-rails - RSpec 功能规范找不到 Rails 引擎的路由

转载 作者:行者123 更新时间:2023-12-01 03:21:51 25 4
gpt4 key购买 nike

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

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

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

最佳答案

这应该有所帮助。确保您有 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

关于ruby-on-rails - RSpec 功能规范找不到 Rails 引擎的路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44570941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com