LoginSignup
6
4

More than 3 years have passed since last update.

RSpecでApplicationHelperのメソッドを使う

Last updated at Posted at 2020-05-10

テストファイルでも、ヘルパーメソッドを使いたいときってありますよね。

ApplicationHelperを使いたい場合を想定します。

複数のファイルで利用したいとき

spec/rails_helper.rbApplicationHelperincludeすればよいです。

spec/rails_helper.rb
include ApplicationHelper

specファイルは基本的にどれもトップレベルでrequire rails_helperしているはずなので、spec/rails_helper.rbincludeしてあげっちゃえばよいわけです。

1つのファイルでのみ利用したいとき

対象のテストファイルでApplicationHelperincludeすればよいです。
例にSystemSpecを挙げます。

spec/systems/sample_spec.rb
include ApplicationHelper

もしincludeを忘れたら

もしincludeを忘れたら、下記のエラー文で怒られます。

NoMethodError:
undefined method `method_name' for #<RSpec::ExampleGroups::SiteLayout:0x00007f85f17a5a70>
6
4
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
6
4