ユーザーに定義されたメタデータ
User-defined metadata
あるexampleグループまたexampleへユーザーが定義したメタデータを添付することができる。describe
、context
または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.
さらに、あなたは適切なシンボルだけを使ってメタデータを明示することができる。describe
、context
または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