コード日進月歩

しんくうの技術的な小話、メモ、つれづれ、など

RSpecのbefore/afterのパラメータをざっと表で書き表す

パターンに関して把握してなかったのでメモ

環境

$ bundle exec rspec -v
RSpec 3.8
  - rspec-core 3.8.0
  - rspec-expectations 3.8.2
  - rspec-mocks 3.8.0
  - rspec-rails 3.8.1
  - rspec-support 3.8.0

説明

書き方 挙動 他の指定の実行順番
before(:suite) rspecの実行時に1回のみ、RSpec.configureの中でしか使えない 1
before(:context) describeやcontextなどの単位で1回だけ行われる 2
before(:example) 各example、itの実行前に行われる 3
after(:example) 各example、itの実行後に行われる 4
after(:context) describeやcontextなどの単位で範囲のものが終わったあとに1回だけ実行される 5
after(:suite) すべてのrspecの実行後に実行される 6

:suite は範囲外に書いてもエラーにはならないが無視される

参考リンク