テストを制限する

>### Inclusion filters

inclusion filterを宣言することによってどのexampleを実行するかを制限できる。最も一般的な使用例は特定の問題に集中しながらexampleのsubsetに焦点を当てることです。あなたはシンボルだけを使ってメタデータを明示もできる。
>You can constrain which examples are run by declaring an inclusion filter. The most common use case is to focus on a subset of examples as you’re focused on a particular problem. You can also specify metadata using only symbols.

>### Background
```rb
RSpec.configure do |c|
  c.filter_run_including :focus => true
end
```
exampleに焦点を当てる
>### Focus on an example
```rb
require "spec_helper"

RSpec.describe "something" do
  it "does one thing" do
  end

  it "does another thing", :focus => true do
  end
end
```
```
**********: % rspec spec/models/user_spec.rb --format doc
...
Run options: include {:focus=>true}

something
  does another thing

Finished in 0.0231 seconds (files took 1.15 seconds to load)
1 example, 0 failures
```
```rb
RSpec.describe "something" do
  it "does one thing" do
  end

  it "does another thing", :focus do # シンボルだけでもできる
  end
end
```
```
**********: % rspec spec/models/user_spec.rb --format doc
...
Run options: include {:focus=>true}

something
  does another thing

Finished in 0.0231 seconds (files took 1.15 seconds to load)
1 example, 0 failures
```
グループに焦点を当てる
>### Focus on a group
```rb
require "spec_helper"

RSpec.describe "group 1", :focus => true do
  it "group 1 example 1" do
  end

  it "group 1 example 2" do
  end
end
RSpec.describe "group 2" do
  it "group 2 example 1" do
  end
end
```
```
**********: % rspec spec/models/user_spec.rb --format doc
...
Run options: include {:focus=>true}

group 1
  group 1 example 1
  group 1 example 2

Finished in 0.02447 seconds (files took 1.22 seconds to load)
2 examples, 0 failures

```
一致しないexampleグループ内の`before/after(:context)`フックは実行されない
>### before/after(:context) hooks in unmatched example group are not run
```rb
require "spec_helper"

RSpec.describe "group 1", :focus => true do
  before(:context) { puts "before all in focused group" }
  after(:context)  { puts "after all in focused group"  }

  it "group 1 example" do
  end
end

RSpec.describe "group 2" do
  before(:context) { puts "before all in unfocused group" }
  after(:context)  { puts "after all in unfocused group"  }

  context "context 1" do
    it "group 2 context 1 example 1" do
    end
  end
end
```
```
**********: % rspec spec/models/user_spec.rb --format doc
...
Run options: include {:focus=>true}
group 1
before all in focused group
  group 1 example
after all in focused group

Finished in 0.02372 seconds (files took 1.15 seconds to load)
1 example, 0 failures
```
メタデータとしてシンボルを使う
>### Use symbols as metadata
```rb
RSpec.configure do |c|
  c.filter_run :current_example
end

RSpec.describe "something" do
  it "does one thing" do
  end

  it "does another thing", :current_example do
  end
end
```

https://rspec.info/features/3-13/rspec-core/filtering/inclusion-filters/

```rb
# filter_run_including(*args) ⇒ void # argsがキー、voidがバリュー
 Also known as: filter_run
```
`key/value`をinclusion_filterへ加える。もし引数に任意のハッシュの一部でないシンボルが含まれていたらならば、各シンボルはハッシュ内のキーとして扱われ、値はtrueになる。
>Adds key/value pairs to the inclusion_filter. If args includes any symbols that are not part of the hash, each symbol is treated as a key in the hash with the value true.

https://rubydoc.info/github/rspec/rspec-core/RSpec/Core/Configuration#:~:text=Adds%20key/value%20pairs%20to%20the%20inclusion_filter.%20If%20args%20includes%20any%20symbols%20that%20are%20not%20part%20of%20the%20hash%2C%20each%20symbol%20is%20treated%20as%20a%20key%20in%20the%20hash%20with%20the%20value%20true

```rb
RSpec.configure do |c|
  c.filter_run_including :focus
end
```
```rb
RSpec.describe "group 1", :focus => true do
  before(:context) { puts "before all in focused group" }
  after(:context)  { puts "after all in focused group"  }

  it "group 1 example" do
  end
end

RSpec.describe "group 2" do
  before(:context) { puts "before all in unfocused group" }
  after(:context)  { puts "after all in unfocused group"  }

  context "context 1" do
    it "group 2 context 1 example 1" do
    end
  end
end
```
```
Run options: include {:focus=>true}

group 1
before all in focused group
  group 1 example
after all in focused group

Finished in 0.02436 seconds (files took 1.24 seconds to load)
1 example, 0 failures
```