- RSpec 教程
- RSpec - 主頁
- RSpec - 簡介
- RSpec - 基本句法
- RSpec - 編寫規範
- RSpec - 匹配器
- RSpec - 測試替身
- RSpec - 存根
- RSpec - 鉤子
- RSpec - 標記
- RSpec - 主題
- RSpec - 輔助工具
- RSpec - 元資料
- RSpec - 篩選
- RSpec - 期望
- RSpec 資源
- RSpec - 快速指南
- RSpec - 有用資源
- RSpec - 討論
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 塊說明 + 其包含的 context 塊說明 + it 塊的說明組合而成的句子。
需要注意的是,這三個字串加在一起就像一個正常的英語句子。. .這是 RSpec 背後的一個理念,即測試聽起來像行為的英文說明。
廣告