LoginSignup
0
1

More than 5 years have passed since last update.

ヘルパーなどで省略したパスをテストするメモ

Last updated at Posted at 2017-08-22

ヘルパーでパスのパラメータを省略して記述した場合のテストの書き方のメモです。
デコレータなどでも同様です。

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
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1