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

ユーザーに定義されたメタデータ

User-defined metadata

あるexampleグループまたexampleへユーザーが定義したメタデータを添付することができる。describecontextまたはitに最後の引数(ブロックの前の)としてハッシュを渡す。RSpecはメタデータを基づいて特定のグループまたはexampleのみ適用される多くの構成オプションをサポートします。

You can attach user-defined metadata to any example group or example. Pass a hash as the last argument (before the block) to describe, context or it. RSpec supports many configuration options that apply only to certain examples or groups based on the metadata.

グループまたはサブグループにある任意のexampleまたは任意のexampleグループから定義されたメタデータは使用可能です(そしてオーバーライドされます)

Metadata defined on an example group is available (and can be overridden) by any sub-group or from any example in that group or a sub-group.

さらに、あなたは適切なシンボルだけを使ってメタデータを明示することができる。describecontextまたはitに引数として渡されるシンボルそれぞれがメタデータハッシュのキーになり、対応する値はtrueとなる。

In addition, you can specify metadata using just symbols. Each symbol passed as an argument to describe, context or it will be a key in the metadata hash, with a corresponding value of true.

ハッシュを使ってグループメタデータを定義する

Define group metadata using a hash

RSpec.describe "a group with user-defined metadata", :foo => 17 do
  it 'has access to the metadata in the example' do |example|
    expect(example.metadata[:foo]).to eq(17)
  end

  it 'does not have access to metadata defined on sub-groups' do |example|
    expect(example.metadata).not_to include(:bar)
  end

  describe 'a sub-group with user-defined metadata', :bar => 12 do # サブグループからメタデータを定義 describeに渡されている
    it 'has access to the sub-group metadata' do |example|
      expect(example.metadata[:bar]).to eq(12)
    end

    it 'also has access to metadata defined on parent groups' do |example|
      expect(example.metadata[:foo]).to eq(17)
    end
  end
end
Run options: include {:full_description=>/a\ group\ with\ user\-defined\ metadata/}
4 examples, 0 failures, 4 passed

ハッシュを使ってexampleのメタデータを定義する

Define example metadata using a hash

RSpec.describe "a group with no user-defined metadata" do
  it 'has an example with metadata', :foo => 17 do |example| # itで定義されるメタデータ
    expect(example.metadata[:foo]).to eq(17)
    expect(example.metadata).not_to include(:bar)
  end

  it 'has another example with metadata', :bar => 12, :bazz => 33 do |example|
    expect(example.metadata[:bar]).to eq(12)
    expect(example.metadata[:bazz]).to eq(33)
    expect(example.metadata).not_to include(:foo)
  end
end
Run options: include {:full_description=>/a\ group\ with\ no\ user\-defined\ metadata/}
2 examples, 0 failures, 2 passed

ユーザーが定義したメタデータをオーバーライドする

Override user-defined metadata

RSpec.describe "a group with user-defined metadata", :foo => 'bar' do
  it 'can be overridden by an example', :foo => 'bazz' do |example|
    expect(example.metadata[:foo]).to eq('bazz')
  end

  describe "a sub-group with an override", :foo => 'goo' do # オーバーライドしている
    it 'can be overridden by a sub-group' do |example|
      expect(example.metadata[:foo]).to eq('goo')
    end
  end
end
Run options: include {:full_description=>/a\ group\ with\ user\-defined\ metadata/}
2 examples, 0 failures, 2 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?