📜  RSpec-元数据

📅  最后修改于: 2020-12-06 10:57:56             🧑  作者: Mango


RSpec是一种灵活而强大的工具。 RSpec中的元数据功能也不例外。元数据通常指“关于数据的数据”。在RSpec中,这意味着有关您的describecontext及其block的数据

让我们看一个例子-

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: #<:core::example::executionresult:0x00000002befd50>
block: #<0x00000002bf81a8>
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=>#<:core::example::executionresult:>, :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=>#<0x00000002baff70>
      , :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
0x00000002baff70>0x00000002bf81a8>

最有可能的是,您不需要使用所有这些元数据,而是查看完整的描述值-

具有元数据变量的示例组和具有另一个变量的上下文可以访问上下文块中的元数据变量。

这是一个由describe块描述+其包含的上下文块描述+ it块的描述创建的句子。

这里有趣的是,这三个字符串一起读起来就像一个普通的英语句子。 。 。这是RSpec背后的想法之一,它的测试听起来像是对行为的英语描述。