Rspec-metadata
提供:Dev Guides
RSpec-メタデータ
RSpecは柔軟で強力なツールです。 RSpecのメタデータ機能も例外ではありません。 メタデータは一般に「データに関するデータ」を指します。 RSpecでは、これは describe 、 context および* itブロック*に関するデータを意味します。
例を見てみましょう-
RSpec.describe "An Example Group with a metadata variable", :foo => 17 do
context 'and a context with another variable', :bar => 12 do
it 'can access the metadata variable of the outer Example Group' do |example|
expect(example.metadata[:foo]).to eq(17)
end
it 'can access the metadata variable in the context block' do |example|
expect(example.metadata[:bar]).to eq(12)
end
end
end
上記のコードを実行すると、この出力が表示されます-
..
Finished in 0.002 seconds (files took 0.11301 seconds to load)
2 examples, 0 failures
メタデータは、RSpecファイル内のさまざまなスコープで変数を割り当てる方法を提供します。 example.metadata変数は、例および例グループに関する他の情報を含むRubyハッシュです。
たとえば、上記のコードを次のように書き換えてみましょう-
RSpec.describe "An Example Group with a metadata variable", :foo => 17 do
context 'and a context with another variable', :bar => 12 do
it 'can access the metadata variable in the context block' do |example|
expect(example.metadata[:foo]).to eq(17)
expect(example.metadata[:bar]).to eq(12)
example.metadata.each do |k,v|
puts "#{k}: #{v}"
end
end
end
このコードを実行すると、example.metadataハッシュ内のすべての値が表示されます-
.execution_result: #<RSpec::Core::Example::ExecutionResult:0x00000002befd50>
block: #<Proc:0x00000002bf81a8@C:/rspec_tutorial/spec/metadata_spec.rb:7>
description_args: ["can access the metadata variable in the context block"]
description: can access the metadata variable in the context block
full_description: An Example Group with a metadata variable and a context
with another variable can access the metadata variable in the context block
described_class:
file_path: ./metadata_spec.rb
line_number: 7
location: ./metadata_spec.rb:7
absolute_file_path: C:/rspec_tutorial/spec/metadata_spec.rb
rerun_file_path: ./metadata_spec.rb
scoped_id: 1:1:2
foo: 17
bar: 12
example_group:
{:execution_result=>#<RSpec::Core::Example::ExecutionResult:
0x00000002bfa0e8>, :block=>#<
Proc:0x00000002bfac00@C:/rspec_tutorial/spec/metadata_spec.rb:2>,
:description_args=>["and a context with another variable"],
:description=>"and a context with another variable",
:full_description=>"An Example Group with a metadata variable
and a context with another variable", :described_class=>nil,
:file_path=>"./metadata_spec.rb",
:line_number=>2, :location=>"./metadata_spec.rb:2",
:absolute_file_path=>"C:/rspec_tutorial/spec/metadata_spec.rb",
:rerun_file_path=>"./metadata_spec.rb",
:scoped_id=>"1:1", :foo=>17, :parent_example_group=>
{:execution_result=>#<
RSpec::Core::Example::ExecutionResult:0x00000002c1f690>,
:block=>#<Proc:0x00000002baff70@C:/rspec_tutorial/spec/metadata_spec.rb:1>
, :description_args=>["An Example Group with a metadata variable"],
:description=>"An Example Group with a metadata variable",
:full_description=>"An Example Group with a metadata variable",
:described_class=>nil, :file_path=>"./metadata_spec.rb",
:line_number=>1, :location=>"./metadata_spec.rb:1",
:absolute_file_path=>
"C:/rspec_tutorial/spec/metadata_spec.rb",
:rerun_file_path=>"./metadata_spec.rb",
:scoped_id=>"1", :foo=>17},
:bar=>12}shared_group_inclusion_backtrace: []
last_run_status: unknown .
.
Finished in 0.004 seconds (files took 0.11101 seconds to load)
2 examples, 0 failures
ほとんどの場合、このメタデータのすべてを使用する必要はありませんが、完全な説明値を見てください-
メタデータ変数と別の変数を持つコンテキストを持つサンプルグループは、コンテキストブロック内のメタデータ変数にアクセスできます。
これは、describeブロックの説明+含まれるコンテキストブロックの説明+ * itブロック*の説明から作成された文です。
ここで興味深いのは、これら3つの文字列が一緒になって通常の英語の文のように読めることです。 . . これはRSpecの背後にある考え方の1つであり、動作の英語の説明のように聞こえるテストを持っています。