【RSpec】before フックのサンプルメモ

サンプル実装。以下にbeforeフックを色々と仕込んでみる。

```ruby:sample.rb
RSpec.describe "Basic" do
  context "context1" do
    it "test1" do
      pp "it test1"
    end

    it "test2" do
      pp "it test2"
    end
  end

  context "context2" do
    it "test3" do
      pp "it test3"
    end

    it "test4" do
      pp "it test4"
    end
  end
end
```

# beforeをContextの外側に配置

## :example, デフォルト
:exmapleは各example(it)毎に実行される。
何も指定しないとこの動作になる。

### デフォルト

```ruby:before1.rb
RSpec.describe "Basic" do
  before do
    pp "before"
  end

  context "context1" do
    it "test1" do
      pp "it test1"
    end

    it "test2" do
      pp "it test2"
    end
  end

  context "context2" do
    it "test3" do
      pp "it test3"
    end

    it "test4" do
      pp "it test4"
    end
  end
end

```

```ruby:実行結果
 % be rspec spec/before1.rb      
"before"
"it test1"
."before"
"it test2"
."before"
"it test3"
."before"
"it test4"
```

### :example

```ruby:before1-context.rb
RSpec.describe "Basic" do
  before(:example) do
    pp "before"
  end

  context "context1" do
    it "test1" do
      pp "it test1"
    end

    it "test2" do
      pp "it test2"
    end
  end

  context "context2" do
    it "test3" do
      pp "it test3"
    end

    it "test4" do
      pp "it test4"
    end
  end
end
```

デフォルト（何も指定しない）と同じ結果になる。

```ruby:実行結果
"before"
"it test1"
."before"
"it test2"
."before"
"it test3"
."before"
"it test4"
.
```

## :context

```before1-context.rb
RSpec.describe "Basic" do
  before(:context) do
    pp "before"
  end

  context "context1" do
    it "test1" do
      pp "it test1"
    end

    it "test2" do
      pp "it test2"
    end
  end

  context "context2" do
    it "test3" do
      pp "it test3"
    end

    it "test4" do
      pp "it test4"
    end
  end
end
```

Context全体に対して1回だけ実行される

```ruby:実行結果
 % be rspec spec/before1-context.rb 
"before"
"it test1"
."it test2"
."it test3"
."it test4"
```

# beforeをContextの内側に配置

各Contextの内側にbeforeを配置してみる

## :example, デフォルト
:exmapleは各example(it)毎に実行される。
何も指定しないとこの動作になる。

### デフォルト

```before2.rb
RSpec.describe "Basic" do
  context "context1" do
    before do
      pp "before1"
    end

    it "test1" do
      pp "it test1"
    end

    it "test2" do
      pp "it test2"
    end
  end

  context "context2" do
    before do
      pp "before2"
    end

    it "test3" do
      pp "it test3"
    end

    it "test4" do
      pp "it test4"
    end
  end
end
```

各example毎にそれぞれのContext内のbeforeが実行される

```ruby:実行結果
 % be rspec spec/before2.rb        
"before1"
"it test1"
."before1"
"it test2"
."before2"
"it test3"
."before2"
"it test4"
.
```

### example

```before2-example.rb
RSpec.describe "Basic" do
  context "context1" do
    before(:example) do
      pp "before1"
    end

    it "test1" do
      pp "it test1"
    end

    it "test2" do
      pp "it test2"
    end
  end

  context "context2" do
    before(:example) do
      pp "before2"
    end

    it "test3" do
      pp "it test3"
    end

    it "test4" do
      pp "it test4"
    end
  end
end
```

デフォルトと同じ

```rb:実行結果
 % be rspec spec/before2-example.rb 
"before1"
"it test1"
."before1"
"it test2"
."before2"
"it test3"
."before2"
"it test4"
.
```

### :context

```before2-context.rb
RSpec.describe "Basic" do
  context "context1" do
    before(:context) do
      pp "before1"
    end

    it "test1" do
      pp "it test1"
    end

    it "test2" do
      pp "it test2"
    end
  end

  context "context2" do
    before(:context) do
      pp "before2"
    end

    it "test3" do
      pp "it test3"
    end

    it "test4" do
      pp "it test4"
    end
  end
end
```

各Conetxtで1回だけ実行される

```rb:実行結果
 % be rspec spec/before2-context.rb 
"before1"
"it test1"
."it test2"
."before2"
"it test3"
."it test4"
.

```

# 参考

Rspec3.1のbefore,afterフックが実行されるタイミング - Qiita https://qiita.com/syunk38/items/e6cc1282c5479555f748

`before` and `after` hooks - Hooks - RSpec Core - RSpec - Relish https://relishapp.com/rspec/rspec-core/docs/hooks/before-and-after-hooks