- RSpec - Expectations
- RSpec - Filtering
- RSpec - Metadata
- RSpec - Helpers
- RSpec - Subjects
- RSpec - Tags
- RSpec - Hooks
- RSpec - Stubs
- RSpec - Test Doubles
- RSpec - Matchers
- RSpec - Writing Specs
- RSpec - Basic Syntax
- RSpec - Introduction
- RSpec - Home
RSpec Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
RSpec - Metadata
RSpec is a flexible and powerful tool. The Metadata functionapty in RSpec is no exception. Metadata generally refers to “data about data”. In RSpec, this means data about your describe, context and it blocks.
Let’s take a look at an example −
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
When you run the above code, you will see this output −
.. Finished in 0.002 seconds (files took 0.11301 seconds to load) 2 examples, 0 failures
Metadata provides a way to assign variables at various scopes within your RSpec files. The example.metadata variable is a Ruby hash which contains other information about your Examples and Example groups.
For instance, let’s rewrite the above code to look pke this −
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
When we run this code, we see all of the values in the example.metadata hash −
.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 pne_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", :pne_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", :pne_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
Most pkely, you will not need to use all of this metadata, but look at the full description value −
An Example Group with a metadata variable and a context with another variable can access the metadata variable in the context block.
This is a sentence created from the describe block description + its contained context block description + the description for the it block.
What is interesting to note here is that, these three strings together read pke a normal Engpsh sentence. . . which is one of the ideas behind RSpec, having tests that sound pke Engpsh descriptions of behavior.
Advertisements