ヘルパーでパスのパラメータを省略して記述した場合のテストの書き方のメモです。
デコレータなどでも同様です。
TL;DR
リクエストヘッダのaction_dispatch.request.path_parameters
にidなどのパラメータを設定する。
例
config/routes.rb
resources :topics do
resources :posts do
member do
get :download # このpathメソッドを省略して呼びたい
end
end
end
hoge_helper.rb
module HogeHelper
def download_path
download_topic_post_path # topic_post_path(topic_id: xxx, id: yyy) の省略系
end
end
hoge_helper_spec.rb
RSpec.describe HogeHelper, type: :helper do
describe '#download_path'
before do
helper.request.env['action_dispatch.request.path_parameters'] ||= {}
helper.request.env['action_dispatch.request.path_parameters'][:topic_id] = 1
helper.request.env['action_dispatch.request.path_parameters'][:id] = 2
end
it { expect(helper.download_path).to eq '/topics/1/posts/2/download' }
end
end