LoginSignup
68
55

More than 5 years have passed since last update.

Rspec3.1のbefore,afterフックが実行されるタイミング

Last updated at Posted at 2016-04-22

rspecの実行時にbefore、afterフックが実行されるタイミングがわからずにハマったので調べてみました。

フックの種類

:example,:context,:suiteの三種類。

:example

デフォルトはこちら。各exampleごとに毎回実行
:eachのエイリアス。

実際に試してみる

require 'rails_helper'

RSpec.describe "フックの実行タイミングを調べる" do
  before :example do
    puts 'before実行'
  end

  after :example do
    puts 'after実行'
  end

  it "実行1" do
    puts 'example1'
  end

  it "実行2" do
    puts 'example2'
  end

  it "実行3" do
    puts 'example3'
  end
end


実行結果
わかりやすくするため、テスト結果の出力は省略しています。

フックの実行タイミングを調べる
before実行
  example1
after実行
before実行
  example2
after実行
before実行
  example3
after実行

exampleごとにbeforeとafterが呼ばれている。

:context

describe、contextのグループごとに毎回実行
:allのエイリアス。

実際に試してみる

require 'rails_helper'

RSpec.describe "フックの実行タイミングを調べる" do
  context "context" do
    before(:context) do
      puts "before実行"
    end

    after(:context) do
      puts "after実行"
    end

    it "実行1" do
      puts '  example1'
    end

    it "実行2" do
      puts '  example2'
    end

    it "実行3" do
      puts '  example3'
    end
  end
end

実行結果

フックの実行タイミングを調べる
  context
before実行
  example1
  example2
  example3
after実行

contextの開始直後、終了直前で呼ばれているのがわかる。
グループ全体で使う初期値の設定などはここで行うのが良さそう。

:suite

rspecの実行時に一度だけ実行
Rspecのconfigurationに記述する。

実際に試してみる

require 'rails_helper'

RSpec.describe "フックの実行タイミングを調べる" do
  RSpec.configure do |config|
    config.before(:suite) do
      puts "before実行"
    end

    config.after(:suite) do
      puts "after実行"
    end
  end

  it "実行1" do
    puts '  example1'
  end

  it "実行2" do
    puts '  example2'
  end
  it "実行3" do
    puts '  example2'
  end
end


実行結果

before実行

フックの実行タイミングを調べる
  example1
  example2
  example2
after実行

:suiteのみRSpecのconfigurationに記述する
そのためrails_helper.rbやspec_helper.rbにも記述することができる。
specの実行前にbefore、終了後にafterが実行されているのがわかる。

全部いっぺんにやってみる

それぞれ同時に設定することができ、その場合の実行順は

before :suite
before :context
before :example
after  :example
after  :context
after  :suite

となる

実際に試してみる

require 'rails_helper'

RSpec.describe "フックの実行タイミングを調べる" do
  RSpec.configure do |config|
    config.before(:suite) do
      puts "before suite"
    end

    config.after(:suite) do
      puts "after suite"
    end
  end

  before(:example) do
    puts "before example"
  end

  after(:example) do
    puts "after example"
  end

  context "context1" do
    before(:context) do
      puts "before context"
    end

    after(:context) do
      puts "after context"
    end

    it "実行1" do
      puts '  example1'
    end

    it "実行2" do
      puts '  example2'
    end

    it "実行3" do
      puts '  example3'
    end
  end
end

実行結果

before suite

フックの実行タイミングを調べる
  context1
before context
before example
  example1
after example
    実行1
before example
  example2
after example
    実行2
before example
  example3
after example
    実行3
after context
after suite

before :suite
before :context
before :example
after :example
after :context
after :suite

の順に呼ばれていますね!

参考

68
55
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
68
55