类: RSpec::Core::ExampleGroup
- 继承自
- 
      Object
        - Object
- RSpec::Core::ExampleGroup
 
- 定义于
- lib/rspec/core/example_group.rb
概述
ExampleGroup 和 Example 是 rspec-core 的主要结构元素。考虑以下示例
RSpec.describe Thing do
  it "does something" do
  end
end
describe Thing 返回的对象是 ExampleGroup 的子类。it "does something" 返回的对象是 Example 的实例,它充当声明它的 ExampleGroup 实例的包装器。
示例组主体(例如 describe 或 context 块)在 ExampleGroup 的新子类上下文中进行评估。单个示例在它们所属的特定 ExampleGroup 子类的实例上下文中进行评估。
除了这里定义的类方法之外,Hooks、MemoizedHelpers::ClassMethods 和 SharedExampleGroup 中定义了其他有趣的宏。MemoizedHelpers 和 Pending 中定义了可用于示例的其他实例方法。
常量摘要 折叠
- WrongScopeError =当在错误的范围内调用 RSpec API 时引发,例如从示例中而不是从示例组块中调用 before。
- Class.new(NoMethodError) 
元数据 折叠
- 
   .description ⇒ String   当前示例组描述。 
- 
   .metadata ⇒ void   与该组关联的 Metadata 对象。 
- 
   #described_class ⇒ void   返回传递给 describe方法(或别名)的类或模块。
定义示例 折叠
- 
   .example {|Example| ... } ⇒ void   在组中定义一个示例。 
- 
   .fexample {|Example| ... } ⇒ void   使用 :focus => true定义示例的快捷方式。
- 
   .fit {|Example| ... } ⇒ void   使用 :focus => true定义示例的快捷方式。
- 
   .focus {|Example| ... } ⇒ void   使用 :focus => true定义示例的快捷方式。
- 
   .fspecify {|Example| ... } ⇒ void   使用 :focus => true定义示例的快捷方式。
- 
   .it {|Example| ... } ⇒ void   在组中定义一个示例。 
- 
   .pending {|Example| ... } ⇒ void   使用 :pending => true定义示例的快捷方式。
- 
   .skip {|Example| ... } ⇒ void   使用 :skip => true定义示例的快捷方式。
- 
   .specify {|Example| ... } ⇒ void   在组中定义一个示例。 
- 
   .xexample {|Example| ... } ⇒ void   使用 :skip => 'Temporarily skipped with xexample'定义示例的快捷方式。
- 
   .xit {|Example| ... } ⇒ void   使用 :skip => 'Temporarily skipped with xit'定义示例的快捷方式。
- 
   .xspecify {|Example| ... } ⇒ void   使用 :skip => 'Temporarily skipped with xspecify'定义示例的快捷方式。
包含共享示例组 折叠
- 
   .add_example(example) ⇒ void   向示例组添加示例。 
- 
   .include_context(name, *args, &block) ⇒ void   直接将映射到 name的共享内容包含在声明它的组中,而不是it_behaves_like,后者创建了一个嵌套组。
- 
   .include_examples(name, *args, &block) ⇒ void   直接将映射到 name的共享内容包含在声明它的组中,而不是it_behaves_like,后者创建了一个嵌套组。
- 
   .remove_example(example) ⇒ void   从示例组中移除示例。 
类方法摘要 折叠
- 
   .currently_executing_a_context_hook? ⇒ Boolean   如果当前正在执行 before(:context)或after(:context)钩子,则返回 true。
- 
   .id ⇒ String   此示例组的唯一 ID。 
- 
   .run(reporter = RSpec::Core::NullReporter) ⇒ void   运行该组中的所有示例。 
从 Hooks 中包含的方法
after, append_after, around, before, prepend_before
从 MemoizedHelpers::ClassMethods 中包含的方法
从 SharedExampleGroup 中包含的方法
从 Pending 中包含的方法
从 MemoizedHelpers 中包含的方法
#is_expected, #should, #should_not, #subject
类方法详情
.add_example(example) ⇒void
向示例组添加示例
| 367 368 369 370 | # File 'lib/rspec/core/example_group.rb', line 367 def self.add_example(example) reset_memoized examples << example end | 
.currently_executing_a_context_hook? ⇒Boolean
如果当前正在执行 before(:context) 或 after(:context) 钩子,则返回 true。
| 542 543 544 | # File 'lib/rspec/core/example_group.rb', line 542 def self.currently_executing_a_context_hook? @currently_executing_a_context_hook end | 
.description ⇒String
返回当前示例组描述。
| 85 86 87 88 | # File 'lib/rspec/core/example_group.rb', line 85 def self.description description = [:description] RSpec.configuration.format_docstrings_block.call(description) end | 
.example ⇒ void .example(&example_implementation) ⇒ void .example(doc_string, *metadata) ⇒ void .example(doc_string, *metadata, &example_implementation) ⇒ void
在组中定义一个示例。
| 158 | # File 'lib/rspec/core/example_group.rb', line 158 define_example_method :example | 
.fexample ⇒ void .fexample(&example_implementation) ⇒ void .fexample(doc_string, *metadata) ⇒ void .fexample(doc_string, *metadata, &example_implementation) ⇒ void
使用 :focus => true 定义示例的快捷方式。
| 177 | # File 'lib/rspec/core/example_group.rb', line 177 define_example_method :fexample, :focus => true | 
.fit ⇒ void .fit(&example_implementation) ⇒ void .fit(doc_string, *metadata) ⇒ void .fit(doc_string, *metadata, &example_implementation) ⇒ void
使用 :focus => true 定义示例的快捷方式。
| 180 | # File 'lib/rspec/core/example_group.rb', line 180 define_example_method :fit, :focus => true | 
.focus ⇒ void .focus(&example_implementation) ⇒ void .focus(doc_string, *metadata) ⇒ void .focus(doc_string, *metadata, &example_implementation) ⇒ void
使用 :focus => true 定义示例的快捷方式。
| 174 | # File 'lib/rspec/core/example_group.rb', line 174 define_example_method :focus, :focus => true | 
.fspecify ⇒ void .fspecify(&example_implementation) ⇒ void .fspecify(doc_string, *metadata) ⇒ void .fspecify(doc_string, *metadata, &example_implementation) ⇒ void
使用 :focus => true 定义示例的快捷方式。
| 183 | # File 'lib/rspec/core/example_group.rb', line 183 define_example_method :fspecify, :focus => true | 
.id ⇒String
返回此示例组的唯一 ID。在命令行中传递此 ID 可重新运行此确切的示例组。
| 675 676 677 | # File 'lib/rspec/core/example_group.rb', line 675 def self.id Metadata.id_from() end | 
.include_context(name, *args, &block) ⇒void
将映射到 name 的共享内容直接包含在声明它的组中,与 it_behaves_like 不同,后者创建嵌套组。如果给定块,则该块也会在当前上下文中进行计算。
| 343 344 345 | # File 'lib/rspec/core/example_group.rb', line 343 def self.include_context(name, *args, &block) find_and_eval_shared("context", name, caller.first, *args, &block) end | 
.include_examples(name, *args, &block) ⇒void
将映射到 name 的共享内容直接包含在声明它的组中,与 it_behaves_like 不同,后者创建嵌套组。如果给定块,则该块也会在当前上下文中进行计算。
| 353 354 355 | # File 'lib/rspec/core/example_group.rb', line 353 def self.include_examples(name, *args, &block) find_and_eval_shared("examples", name, caller.first, *args, &block) end | 
.it ⇒ void .it(&example_implementation) ⇒ void .it(doc_string, *metadata) ⇒ void .it(doc_string, *metadata, &example_implementation) ⇒ void
在组内定义一个示例。这是定义代码示例的主要 API。
| 161 | # File 'lib/rspec/core/example_group.rb', line 161 define_example_method :it | 
.metadata ⇒void
与该组关联的 Metadata 对象。
| 51 52 53 | # File 'lib/rspec/core/example_group.rb', line 51 def self. @metadata ||= nil end | 
.pending ⇒ void .pending(&example_implementation) ⇒ void .pending(doc_string, *metadata) ⇒ void .pending(doc_string, *metadata, &example_implementation) ⇒ void
定义具有 :pending => true 的示例的快捷方式
| 198 | # File 'lib/rspec/core/example_group.rb', line 198 define_example_method :pending, :pending => true | 
.remove_example(example) ⇒void
从示例组中删除一个示例
| 373 374 375 376 | # File 'lib/rspec/core/example_group.rb', line 373 def self.remove_example(example) reset_memoized examples.delete example end | 
.run(reporter = RSpec::Core::NullReporter) ⇒void
运行该组中的所有示例。
| 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 | # File 'lib/rspec/core/example_group.rb', line 599 def self.run(reporter=RSpec::Core::NullReporter) return if RSpec.world.wants_to_quit reporter.example_group_started(self) should_run_context_hooks = descendant_filtered_examples.any? begin RSpec.current_scope = :before_context_hook run_before_context_hooks(new('before(:context) hook')) if should_run_context_hooks result_for_this_group = run_examples(reporter) results_for_descendants = ordering_strategy.order(children).map { |child| child.run(reporter) }.all? result_for_this_group && results_for_descendants rescue Pending::SkipDeclaredInExample => ex for_filtered_examples(reporter) { |example| example.skip_with_exception(reporter, ex) } true rescue Support::AllExceptionsExceptOnesWeMustNotRescue => ex for_filtered_examples(reporter) { |example| example.fail_with_exception(reporter, ex) } RSpec.world.wants_to_quit = true if reporter.fail_fast_limit_met? false ensure RSpec.current_scope = :after_context_hook run_after_context_hooks(new('after(:context) hook')) if should_run_context_hooks reporter.example_group_finished(self) end end | 
.skip ⇒ void .skip(&example_implementation) ⇒ void .skip(doc_string, *metadata) ⇒ void .skip(doc_string, *metadata, &example_implementation) ⇒ void
定义具有 :skip => true 的示例的快捷方式
| 195 | # File 'lib/rspec/core/example_group.rb', line 195 define_example_method :skip, :skip => true | 
.specify ⇒ void .specify(&example_implementation) ⇒ void .specify(doc_string, *metadata) ⇒ void .specify(doc_string, *metadata, &example_implementation) ⇒ void
在组内定义一个示例。当您的文档字符串在 it 上读起来不好时,很有用。
| 170 | # File 'lib/rspec/core/example_group.rb', line 170 define_example_method :specify | 
.xexample ⇒ void .xexample(&example_implementation) ⇒ void .xexample(doc_string, *metadata) ⇒ void .xexample(doc_string, *metadata, &example_implementation) ⇒ void
定义具有 :skip => 'Temporarily skipped with xexample' 的示例的快捷方式。
| 186 | # File 'lib/rspec/core/example_group.rb', line 186 define_example_method :xexample, :skip => 'Temporarily skipped with xexample' | 
.xit ⇒ void .xit(&example_implementation) ⇒ void .xit(doc_string, *metadata) ⇒ void .xit(doc_string, *metadata, &example_implementation) ⇒ void
定义具有 :skip => 'Temporarily skipped with xit' 的示例的快捷方式。
| 189 | # File 'lib/rspec/core/example_group.rb', line 189 define_example_method :xit, :skip => 'Temporarily skipped with xit' | 
.xspecify ⇒ void .xspecify(&example_implementation) ⇒ void .xspecify(doc_string, *metadata) ⇒ void .xspecify(doc_string, *metadata, &example_implementation) ⇒ void
定义具有 :skip => 'Temporarily skipped with xspecify' 的示例的快捷方式。
| 192 | # File 'lib/rspec/core/example_group.rb', line 192 define_example_method :xspecify, :skip => 'Temporarily skipped with xspecify' | 
实例方法详细信息
#described_class ⇒void
返回传递给 describe 方法(或别名)的类或模块。如果主题不是类或模块,则返回 nil。
| 99 100 101 | # File 'lib/rspec/core/example_group.rb', line 99 def described_class self.class.described_class end |