类: RSpec::Core::ExampleGroup

继承自
Object
  • Object
显示所有
扩展自
Hooks, MemoizedHelpers::ClassMethods, SharedExampleGroup
包含
MemoizedHelpers, Pending
定义于
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 实例的包装器。

示例组主体(例如 describecontext 块)在 ExampleGroup 的新子类上下文中进行评估。单个示例在它们所属的特定 ExampleGroup 子类的实例上下文中进行评估。

除了这里定义的类方法之外,HooksMemoizedHelpers::ClassMethodsSharedExampleGroup 中定义了其他有趣的宏。MemoizedHelpersPending 中定义了可用于示例的其他实例方法。

常量摘要 折叠

WrongScopeError =

当在错误的范围内调用 RSpec API 时引发,例如从示例中而不是从示例组块中调用 before

Class.new(NoMethodError)

元数据 折叠

定义示例 折叠

包含共享示例组 折叠

类方法摘要 折叠

Hooks 中包含的方法

after, append_after, around, before, prepend_before

MemoizedHelpers::ClassMethods 中包含的方法

let, let!, subject, subject!

SharedExampleGroup 中包含的方法

shared_examples

Pending 中包含的方法

#pending, #skip

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。

返回

  • (Boolean)
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

.descriptionString

返回当前示例组描述。

返回

  • (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

.examplevoid .example(&example_implementation) ⇒ void .example(doc_string, *metadata) ⇒ void .example(doc_string, *metadata, &example_implementation) ⇒ void

在组中定义一个示例。

示例

example do
end
example "does something" do
end
example "does something", :slow, :uses_js do
end
example "does something", :with => 'additional metadata' do
end
example "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

重载

  • .example(&example_implementation) ⇒void

    参数

    • example_implementation (Block)

      示例的实现。

  • .example(doc_string, *metadata) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

  • .example(doc_string, *metadata, &example_implementation) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

    • example_implementation (Block)

      示例的实现。

产出

158
# File 'lib/rspec/core/example_group.rb', line 158
define_example_method :example

.fexamplevoid .fexample(&example_implementation) ⇒ void .fexample(doc_string, *metadata) ⇒ void .fexample(doc_string, *metadata, &example_implementation) ⇒ void

使用 :focus => true 定义示例的快捷方式。

示例

fexample do
end
fexample "does something" do
end
fexample "does something", :slow, :uses_js do
end
fexample "does something", :with => 'additional metadata' do
end
fexample "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

重载

  • .fexample(&example_implementation) ⇒void

    参数

    • example_implementation (Block)

      示例的实现。

  • .fexample(doc_string, *metadata) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

  • .fexample(doc_string, *metadata, &example_implementation) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

    • example_implementation (Block)

      示例的实现。

产出

另请参阅

177
# File 'lib/rspec/core/example_group.rb', line 177
define_example_method :fexample, :focus => true

.fitvoid .fit(&example_implementation) ⇒ void .fit(doc_string, *metadata) ⇒ void .fit(doc_string, *metadata, &example_implementation) ⇒ void

使用 :focus => true 定义示例的快捷方式。

示例

fit do
end
fit "does something" do
end
fit "does something", :slow, :uses_js do
end
fit "does something", :with => 'additional metadata' do
end
fit "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

重载

  • .fit(&example_implementation) ⇒void

    参数

    • example_implementation (Block)

      示例的实现。

  • .fit(doc_string, *metadata) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

  • .fit(doc_string, *metadata, &example_implementation) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

    • example_implementation (Block)

      示例的实现。

产出

另请参阅

180
# File 'lib/rspec/core/example_group.rb', line 180
define_example_method :fit,      :focus => true

.focusvoid .focus(&example_implementation) ⇒ void .focus(doc_string, *metadata) ⇒ void .focus(doc_string, *metadata, &example_implementation) ⇒ void

使用 :focus => true 定义示例的快捷方式。

示例

focus do
end
focus "does something" do
end
focus "does something", :slow, :uses_js do
end
focus "does something", :with => 'additional metadata' do
end
focus "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

重载

  • .focus(&example_implementation) ⇒void

    参数

    • example_implementation (Block)

      示例的实现。

  • .focus(doc_string, *metadata) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

  • .focus(doc_string, *metadata, &example_implementation) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

    • example_implementation (Block)

      示例的实现。

产出

另请参阅

174
# File 'lib/rspec/core/example_group.rb', line 174
define_example_method :focus,    :focus => true

.fspecifyvoid .fspecify(&example_implementation) ⇒ void .fspecify(doc_string, *metadata) ⇒ void .fspecify(doc_string, *metadata, &example_implementation) ⇒ void

使用 :focus => true 定义示例的快捷方式。

示例

fspecify do
end
fspecify "does something" do
end
fspecify "does something", :slow, :uses_js do
end
fspecify "does something", :with => 'additional metadata' do
end
fspecify "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

重载

  • .fspecify(&example_implementation) ⇒void

    参数

    • example_implementation (Block)

      示例的实现。

  • .fspecify(doc_string, *metadata) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

  • .fspecify(doc_string, *metadata, &example_implementation) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

    • example_implementation (Block)

      示例的实现。

产出

另请参阅

183
# File 'lib/rspec/core/example_group.rb', line 183
define_example_method :fspecify, :focus => true

.idString

返回此示例组的唯一 ID。在命令行中传递此 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

.itvoid .it(&example_implementation) ⇒ void .it(doc_string, *metadata) ⇒ void .it(doc_string, *metadata, &example_implementation) ⇒ void

在组内定义一个示例。这是定义代码示例的主要 API。

示例

it do
end
it "does something" do
end
it "does something", :slow, :uses_js do
end
it "does something", :with => 'additional metadata' do
end
it "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

重载

  • .it(&example_implementation) ⇒void

    参数

    • example_implementation (Block)

      示例的实现。

  • .it(doc_string, *metadata) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

  • .it(doc_string, *metadata, &example_implementation) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

    • example_implementation (Block)

      示例的实现。

产出

161
# File 'lib/rspec/core/example_group.rb', line 161
define_example_method :it

.metadatavoid

与该组关联的 Metadata 对象。

另请参阅

51
52
53
# File 'lib/rspec/core/example_group.rb', line 51
def self.
  @metadata ||= nil
end

.pendingvoid .pending(&example_implementation) ⇒ void .pending(doc_string, *metadata) ⇒ void .pending(doc_string, *metadata, &example_implementation) ⇒ void

定义具有 :pending => true 的示例的快捷方式

示例

pending do
end
pending "does something" do
end
pending "does something", :slow, :uses_js do
end
pending "does something", :with => 'additional metadata' do
end
pending "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

重载

  • .pending(&example_implementation) ⇒void

    参数

    • example_implementation (Block)

      示例的实现。

  • .pending(doc_string, *metadata) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

  • .pending(doc_string, *metadata, &example_implementation) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

    • example_implementation (Block)

      示例的实现。

产出

另请参阅

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

.skipvoid .skip(&example_implementation) ⇒ void .skip(doc_string, *metadata) ⇒ void .skip(doc_string, *metadata, &example_implementation) ⇒ void

定义具有 :skip => true 的示例的快捷方式

示例

skip do
end
skip "does something" do
end
skip "does something", :slow, :uses_js do
end
skip "does something", :with => 'additional metadata' do
end
skip "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

重载

  • .skip(&example_implementation) ⇒void

    参数

    • example_implementation (Block)

      示例的实现。

  • .skip(doc_string, *metadata) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

  • .skip(doc_string, *metadata, &example_implementation) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

    • example_implementation (Block)

      示例的实现。

产出

另请参阅

195
# File 'lib/rspec/core/example_group.rb', line 195
define_example_method :skip,     :skip => true

.specifyvoid .specify(&example_implementation) ⇒ void .specify(doc_string, *metadata) ⇒ void .specify(doc_string, *metadata, &example_implementation) ⇒ void

在组内定义一个示例。当您的文档字符串在 it 上读起来不好时,很有用。

示例

RSpec.describe MyClass do
  specify "#do_something is deprecated" do
    # ...
  end
end
specify do
end
specify "does something" do
end
specify "does something", :slow, :uses_js do
end
specify "does something", :with => 'additional metadata' do
end
specify "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

重载

  • .specify(&example_implementation) ⇒void

    参数

    • example_implementation (Block)

      示例的实现。

  • .specify(doc_string, *metadata) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

  • .specify(doc_string, *metadata, &example_implementation) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

    • example_implementation (Block)

      示例的实现。

产出

170
# File 'lib/rspec/core/example_group.rb', line 170
define_example_method :specify

.xexamplevoid .xexample(&example_implementation) ⇒ void .xexample(doc_string, *metadata) ⇒ void .xexample(doc_string, *metadata, &example_implementation) ⇒ void

定义具有 :skip => 'Temporarily skipped with xexample' 的示例的快捷方式。

示例

xexample do
end
xexample "does something" do
end
xexample "does something", :slow, :uses_js do
end
xexample "does something", :with => 'additional metadata' do
end
xexample "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

重载

  • .xexample(&example_implementation) ⇒void

    参数

    • example_implementation (Block)

      示例的实现。

  • .xexample(doc_string, *metadata) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

  • .xexample(doc_string, *metadata, &example_implementation) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

    • example_implementation (Block)

      示例的实现。

产出

另请参阅

186
# File 'lib/rspec/core/example_group.rb', line 186
define_example_method :xexample, :skip => 'Temporarily skipped with xexample'

.xitvoid .xit(&example_implementation) ⇒ void .xit(doc_string, *metadata) ⇒ void .xit(doc_string, *metadata, &example_implementation) ⇒ void

定义具有 :skip => 'Temporarily skipped with xit' 的示例的快捷方式。

示例

xit do
end
xit "does something" do
end
xit "does something", :slow, :uses_js do
end
xit "does something", :with => 'additional metadata' do
end
xit "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

重载

  • .xit(&example_implementation) ⇒void

    参数

    • example_implementation (Block)

      示例的实现。

  • .xit(doc_string, *metadata) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

  • .xit(doc_string, *metadata, &example_implementation) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

    • example_implementation (Block)

      示例的实现。

产出

另请参阅

189
# File 'lib/rspec/core/example_group.rb', line 189
define_example_method :xit,      :skip => 'Temporarily skipped with xit'

.xspecifyvoid .xspecify(&example_implementation) ⇒ void .xspecify(doc_string, *metadata) ⇒ void .xspecify(doc_string, *metadata, &example_implementation) ⇒ void

定义具有 :skip => 'Temporarily skipped with xspecify' 的示例的快捷方式。

示例

xspecify do
end
xspecify "does something" do
end
xspecify "does something", :slow, :uses_js do
end
xspecify "does something", :with => 'additional metadata' do
end
xspecify "does something" do |ex|
  # ex is the Example object that contains metadata about the example
end

重载

  • .xspecify(&example_implementation) ⇒void

    参数

    • example_implementation (Block)

      示例的实现。

  • .xspecify(doc_string, *metadata) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

  • .xspecify(doc_string, *metadata, &example_implementation) ⇒void

    参数

    • doc_string (String)

      示例的文档字符串。

    • metadata (Array<Symbol>, Hash)

      示例的元数据。符号将转换为具有 true 值的哈希条目。

    • example_implementation (Block)

      示例的实现。

产出

另请参阅

192
# File 'lib/rspec/core/example_group.rb', line 192
define_example_method :xspecify, :skip => 'Temporarily skipped with xspecify'

实例方法详细信息

#described_classvoid

返回传递给 describe 方法(或别名)的类或模块。如果主题不是类或模块,则返回 nil。

示例

RSpec.describe Thing do
  it "does something" do
    described_class == Thing
  end
end
99
100
101
# File 'lib/rspec/core/example_group.rb', line 99
def described_class
  self.class.described_class
end