我正在使用rails v5.1.0
和rspec-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
答案 0 :(得分: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
答案 1 :(得分:0)
对于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