0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

aroundフックの特徴

Posted at

exampleをブロック処理の引数とする

aroundフックはprocとして振る舞うことを拡張したブロックの引数としてexampleを受理する。これによりあなたはexampleの前後に実行されるべきコードを定義する。もちろん、あなたはフックの前後で同じことができる。そしてその方がスッキリする方が多い。

around hooks receive the example as a block argument, extended to behave as a proc. This lets you define code that should be executed before and after the example. Of course, you can do the same thing with before and after hooks; and it’s often cleaner to do so.

around()へ渡されたブロック内のprocとしてexampleを使う

Use the example as a proc within the block passed to around()

class Database
  def self.transaction
    puts "open transaction"
    yield
    puts "close transaction"
  end
end

RSpec.describe "around filter" do
  around(:example) do |example|
    Database.transaction(&example)
  end

  it "gets run in order" do
    puts "run the example"
  end
end
open transaction
run the example
close transaction
1 example, 0 failures, 1 passed
  • procに関しては後から勉強するとして流れてとしてはexampleが実行されるたびに呼び出せるみたいだ。
RSpec.describe "around filter" do
  around(:example) do |example|
    Database.transaction(&example)
  end

  it "gets run in order" do
    puts "run the example"
  end

  it "gets run in order" do
    puts "run the example"
  end
end
Run options: include {:full_description=>/around\ filter/}
open transaction
run the example
close transaction
open transaction
run the example
close transaction
2 examples, 0 failures, 2 passed

run()を使ってexampleを発動する

Invoke the example using run()

Given a file named “example_spec.rb” with:

RSpec.describe "around hook" do
  around(:example) do |example|
    puts "around example before"
    example.run
    puts "around example after"
  end

  it "gets run in order" do
    puts "in the example"
  end
end
around example before
in the example
around example after
1 example, 0 failures, 1 passed

メタデータでexampleにアクセスする

Access the example metadata

Given a file named “example_spec.rb” with:

RSpec.describe "something" do
  around(:example) do |example|
    puts example.metadata[:foo]
    example.run
  end

  it "does something", :foo => "this should show up in the output" do
  end
this should show up in the output
1 example, 0 failures, 1 passed
  • メタデータの文字列を表示させている

aroundフックはたとえexampleが例外を投げたとしても実行を続ける

An around hook continues to run even if the example throws an exception

  RSpec.describe "something" do
    around(:example) do |example|
      puts "around example setup"
      example.run
      puts "around example cleanup"
    end

    it "still executes the entire around hook" do
      fail "the example blows up"
    end
  end
around example setup
around example cleanup

RuntimeError: the example blows up
...
1 example, 1 failure, 0 passed

グローバルなaroundフックを定義する

Define a global around hook

Given a file named “example_spec.rb” with:

RSpec.configure do |c|
  c.around(:example) do |example|
    puts "around example before"
    example.run
    puts "around example after"
  end
end

RSpec.describe "around filter" do
  it "gets run in order" do
    puts "in the example"
  end
end
around example before
in the example
around example after
1 example, 0 failures, 1 passed
  • グループが違っても実行できる

exampleごとのフックはaroundフックよにって包まれる

Per example hooks are wrapped by the around hook

RSpec.describe "around filter" do
  around(:example) do |example|
    puts "around example before"
    example.run
    puts "around example after"
  end

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

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

  it "gets run in order" do
    puts "in the example"
  end
end
around example before
before example
in the example
after example
around example after
1 example, 0 failures, 1 passed

contextのフックはaroundフックによって包まれない

Context hooks are NOT wrapped by the around hook

RSpec.describe "around filter" do
  around(:example) do |example|
    puts "around example before"
    example.run
    puts "around example after"
  end

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

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

  it "gets run in order" do
    puts "in the example"
  end
end
before context
around example before
in the example
around example after
after context
1 example, 0 failures, 1 passed

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?