首页 > 其他分享 >无涯教程-RSpec - 元数据

无涯教程-RSpec - 元数据

时间:2023-11-19 13:31:54浏览次数:39  
标签:教程 rb 无涯 RSpec example context variable spec metadata

RSpec是一种灵活而强大的工具。RSpec中的元数据(Metadata)函数也不例外。元数据通常指“关于数据的数据”。在RSpec中,这意味着有关您的描述,上下文及其块的数据,让无涯教程看一个例子-

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

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

参考链接

https://www.learnfk.com/rspec/rspec-metadata.html

标签:教程,rb,无涯,RSpec,example,context,variable,spec,metadata
From: https://blog.51cto.com/u_14033984/8472550

相关文章

  • 无涯教程-RSpec - Subjects主题
    RSpecsubjets提供了编写简写测试用例的快捷方式。考虑以下代码-classPersonattr_reader:first_name,:last_namedefinitialize(first_name,last_name)@first_name=first_name@last_name=last_nameendenddescribePersondo......
  • 火炬之光2 武器装备属性修改 教程
    先在装备框附魔框等等框里点两下(拿起放下)要改的装备然后在点读取才能读到数据附魔也是同样两个读取左边的是读取百分之多少多少开头的属性右边的是读取传输/convey开头的数据  然后自己改想要的值。改完要附魔1次才能生效 点那按钮即可 也可以去附魔师......
  • 无涯教程-RSpec - Tags标签
    RSpecTags提供了一种在规范文件中运行特定测试的简便方法。假设您只想运行指定测试方法,则可以使用Tags标签实现。describe"HowtorunspecificExampleswithTags"doit'isaslowtest',:slow=>truedosleep10puts'Thistestisslow!'end......
  • 无涯教程-RSpec - Stubs
    如果您已经阅读了RSpecDoubles部分,那么您已经看到了RSpecStubs,它是一种特殊类型的方法,代表现有方法或尚不存在的方法。这是RSpecDoubles部分中的代码-classClassRoomdefinitialize(students)@students=studentsEnddeflist_student_names......
  • 无涯教程-RSpec - 模拟对象
    在本章中,无涯教程将讨论RSpecDoubles,RSpecDouble是一个模拟对象,在代码中模拟系统的另一个对象,方便测试。假设您为学校构建一个应用程序,有一个教室,还有一个学生。类定义如下:classClassRoomdefinitialize(students)@students=studentsenddeflis......
  • 无涯教程-RSpec - 基本语法
    让无涯教程仔细看看HelloWorld示例的代码。首先,如果不清楚,正在测试HelloWorld类的函数。当然,这是一个非常简单的类,仅包含一个方法say_hello()。这又是RSpec代码-describeHelloWorlddocontext“WhentestingtheHelloWorldclass”doit"Thesay_......
  • 小甲鱼Delphi教程37课《读写修改记录型文件》源码
    说明:本程序用delphi10.4重写编写一、最后效果二、源码1unitUnit1;23interface45uses6Winapi.Windows,Winapi.Messages,System.SysUtils,System.Variants,System.Classes,Vcl.Graphics,7Vcl.Controls,Vcl.Forms,Vcl.Dialogs,Vcl.StdCtrls,V......
  • 无涯教程-RSpec - 简介
    行为驱动开发(英语:Behavior-drivendevelopment,缩写BDD)是一种敏捷软件开发的技术,它鼓励软件项目中的开发者、QA和非技术人员或商业参与者之间的协作。BDD最初是由DanNorth在2003年命名,它包括验收测试和客户测试驱动等的极限编程的实践,作为对测试驱动开发的回应。在过去数年里,它得......
  • 无涯教程-D语言 - 封装
    封装是一种面向对象的编程概念,它将数据和将数据操作在一起的函数绑定在一起,并且可以确保不受外界干扰,封装导致了数据隐藏的重要OOP概念。一个类可以包含private,protected和public修饰符,默认情况下,类中定义的所有项目都是private私有的。如-classBox{public:......
  • 无涯教程-D语言 - 继承
    面向对象编程中最重要的概念之一是继承,继承允许使用一个类继承另一个类,这样就可以直接调用父类的公共函数或变量,这使得维护变得更加容易。基类和子类子类通过":"冒号来实现继承基类。classderived-class:base-class考虑如下基类Shape及其派生类Rectangle-importstd.s......