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?

メタデータ、シンボルでフィルターをする

Posted at

任意のメタデータを使ってbefore(:example)フックをフィルターする

Filter before(:example) hooks using arbitrary metadata

RSpec.configure do |config|
  config.before(:example, :foo => :bar) do
    invoked_hooks << :before_example_foo_bar
  end
end

RSpec.describe "a filtered before :example hook" do
  let(:invoked_hooks) { [] }

  describe "group without matching metadata" do
    it "does not run the hook" do
      expect(invoked_hooks).to be_empty
    end

    it "runs the hook for an example with matching metadata", :foo => :bar do
      expect(invoked_hooks).to eq([:before_example_foo_bar])
    end
  end

  describe "group with matching metadata", :foo => :bar do
    it "runs the hook" do
      expect(invoked_hooks).to eq([:before_example_foo_bar])
    end
  end
end
Run options: include {:full_description=>/a\ filtered\ before\ :example\ hook/}
3 examples, 0 failures, 3 passed
  • configureブロックに入れるとメタデータでフィルタイングされる

    it "runs the hook for an example with matching metadata" do
      expect(invoked_hooks).to eq([:before_example_foo_bar])
    end
  end
) a filtered before :example hook group without matching metadata runs the hook for an example with matching metadata
     Failure/Error: expect(invoked_hooks).to eq([:before_example_foo_bar])

       expected: [:before_example_foo_bar]
            got: []

       (compared using ==)

任意のメタデータを使ってafter(:example)をフィルタイングする

Filter after(:example) hooks using arbitrary metadata

RSpec.configure do |config|
  config.after(:example, :foo => :bar) do
    raise "boom!"
  end
end

RSpec.describe "a filtered after :example hook" do
  describe "group without matching metadata" do
    it "does not run the hook" do
      # should pass
    end

    it "runs the hook for an example with matching metadata", :foo => :bar do
      # should fail
    end
  end

  describe "group with matching metadata", :foo => :bar do
    it "runs the hook" do
      # should fail
    end
  end
end

around(:example)版

Filter around(:example) hooks using arbitrary metadata

RSpec.configure do |config|
  config.around(:example, :foo => :bar) do |example|
    order << :before_around_example_foo_bar
    example.run
    expect(order).to eq([:before_around_example_foo_bar, :example])
  end
end

RSpec.describe "a filtered around(:example) hook" do
  let(:order) { [] }

  describe "a group without matching metadata" do
    it "does not run the hook" do
      expect(order).to be_empty
    end

    it "runs the hook for an example with matching metadata", :foo => :bar do
      expect(order).to eq([:before_around_example_foo_bar])
      order << :example
    end
  end

  describe "a group with matching metadata", :foo => :bar do
    it "runs the hook for an example with matching metadata", :foo => :bar do
      expect(order).to eq([:before_around_example_foo_bar])
      order << :example
    end
  end
end

before(:context)を使ってもできる

Filter before(:context) hooks using arbitrary metadata

Given a file named “filterbeforecontexthooksspec.rb” with:

RSpec.configure do |config|
  config.before(:context, :foo => :bar) { @hook = :before_context_foo_bar }
end

RSpec.describe "a filtered before(:context) hook" do
  describe "a group without matching metadata" do
    it "does not run the hook" do
      expect(@hook).to be_nil
    end

    it "runs the hook for a single example with matching metadata", :foo => :bar do
      expect(@hook).to eq(:before_context_foo_bar)
    end

    describe "a nested subgroup with matching metadata", :foo => :bar do
      it "runs the hook" do
        expect(@hook).to eq(:before_context_foo_bar)
      end
    end
  end

  describe "a group with matching metadata", :foo => :bar do
    it "runs the hook" do
      expect(@hook).to eq(:before_context_foo_bar)
    end

    describe "a nested subgroup" do
      it "runs the hook" do
        expect(@hook).to eq(:before_context_foo_bar)
      end
    end
  end
end

同上

Filter after(:context) hooks using arbitrary metadata

Given a file named “filteraftercontexthooksspec.rb” with:

example_msgs = []

RSpec.configure do |config|
  config.after(:context, :foo => :bar) do
    puts "after :context"
  end
end

RSpec.describe "a filtered after(:context) hook" do
  describe "a group without matching metadata" do
    it "does not run the hook" do
      puts "unfiltered"
    end

    it "runs the hook for a single example with matching metadata", :foo => :bar do
      puts "filtered 1"
    end
  end

  describe "a group with matching metadata", :foo => :bar do
    it "runs the hook" do
      puts "filtered 2"
    end
  end

  describe "another group without matching metadata" do
    describe "a nested subgroup with matching metadata", :foo => :bar do
      it "runs the hook" do
        puts "filtered 3"
      end
    end
  end
end

メタデータとしてシンボルを使う

Use symbols as metadata

RSpec.configure do |c|
  c.before(:example, :before_example) { puts "before example" }
  c.after(:example,  :after_example) { puts "after example" }
  c.around(:example, :around_example) do |example|
    puts "around example (before)"
    example.run
    puts "around example (after)"
  end
  c.before(:context, :before_context) { puts "before context" }
  c.after(:context,  :after_context) { puts "after context" }
end

RSpec.describe "group 1", :before_context, :after_context do
  it("") { puts "example 1" }
  it("", :before_example) { puts "example 2" }
  it("", :after_example) { puts "example 3" }
  it("", :around_example) { puts "example 4" }
end
Run options: include {:full_description=>/group\ 1/}
before context
example 1
before example
example 2
example 3
after example
around example (before)
example 4
around example (after)
after context
4 examples, 0 failures, 4 passed
  • 基本的に呼ばれるものは決まっていて、追加で呼べるようにしている

シンボルを使ってフックをフィルタリングする

Filtering hooks using symbols

RSpec.configure do |config|
  config.before(:example, :foo) do
    invoked_hooks << :before_example_foo_bar
  end
end

RSpec.describe "a filtered before :example hook" do
  let(:invoked_hooks) { [] }

  describe "group without a matching metadata key" do
    it "does not run the hook" do
      expect(invoked_hooks).to be_empty
    end

    it "does not run the hook for an example with metadata hash containing the key with a falsey value", :foo => nil do
      expect(invoked_hooks).to be_empty
    end

    it "runs the hook for an example with metadata hash containing the key with a truthy value", :foo => :bar do
      expect(invoked_hooks).to eq([:before_example_foo_bar])
    end

    it "runs the hook for an example with only the key defined", :foo do
      expect(invoked_hooks).to eq([:before_example_foo_bar])
    end
  end

  describe "group with matching metadata key", :foo do
    it "runs the hook" do
      expect(invoked_hooks).to eq([:before_example_foo_bar])
    end
  end
end
Run options: include {:full_description=>/a\ filtered\ before\ :example\ hook/}
5 examples, 0 failures, 5 passed
  • :foo => :uooとランダムに入れても通った シンボルに関して何も知らない
  it "runs the hook for an example with metadata hash containing the key with a truthy value", :foo => :uoo do
      expect(invoked_hooks).to eq([:before_example_foo_bar])
    end
Run options: include {:full_description=>/a\ filtered\ before\ :example\ hook\ group\ without\ a\ matching\ metadata\ key\ runs\ the\ hook\ for\ an\ example\ with\ metadata\ hash\ containing\ the\ key\ with\ a\ truthy\ value/}
1 example, 0 failures, 1 passed

Filtering hooks using a hash

RSpec.configure do |config|
  config.before(:example, :foo => { :bar => :baz, :slow => true }) do
    invoked_hooks << :before_example_foo_bar
  end
end

RSpec.describe "a filtered before :example hook" do
  let(:invoked_hooks) { [] }

  describe "group without matching metadata" do
    it "does not run the hook" do
      expect(invoked_hooks).to be_empty
    end

    it "does not run the hook for an example if only part of the filter matches", :foo => { :bar => :baz } do
      expect(invoked_hooks).to be_empty
    end

    it "runs the hook for an example if the metadata contains all key value pairs from the filter", :foo => { :bar => :baz, :slow => true, :extra => :pair } do
      expect(invoked_hooks).to eq([:before_example_foo_bar])
    end
  end

  describe "group with matching metadata", :foo => { :bar => :baz, :slow => true } do
    it "runs the hook" do
      expect(invoked_hooks).to eq([:before_example_foo_bar])
    end
  end
end
  • ハッシュを消すと失敗する 指定されたハッシュの全てが入っていないとできない
it "runs the hook for an example if the metadata contains all key value pairs from the filter", :foo => {  } do
      expect(invoked_hooks).to eq([:before_example_foo_bar])
    end
0) a filtered before :example hook group without matching metadata runs the hook for an example if the metadata contains all key value pairs from the filter
     Failure/Error: expect(invoked_hooks).to eq([:before_example_foo_bar])

       expected: [:before_example_foo_bar]
            got: []

       (compared using ==)
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?