模块: RSpec::Core::SharedExampleGroup

包含于
ExampleGroup
定义于
lib/rspec/core/shared_example_group.rb

概述

共享示例组允许您定义希望在多个示例组中使用的通用上下文和/或通用示例。

定义后,共享组块将被存储以供以后评估。它稍后可以显式地 (使用 include_examplesinclude_contextit_behaves_like) 或隐式地 (通过匹配元数据) 包含在示例组中。

命名共享示例组的范围基于其定义位置。在示例组中定义的共享组可用于包含在该示例组或任何子示例组中,但不可用于任何父示例组或同级示例组。在顶层定义的共享示例组可以从任何示例组中包含。

在命名空间下定义

模块: TopLevelDSL

实例方法概要 收起

实例方法详情

#shared_examples(name, &block) ⇒ void #shared_examples(name, metadata, &block) ⇒ void 也称为: shared_context, shared_examples_for

存储块以供以后使用。该块将在示例组的上下文中通过 include_examplesinclude_contextit_behaves_like 评估。

示例

shared_examples "auditable" do
  it "stores an audit record on save!" do
    expect { auditable.save! }.to change(Audit, :count).by(1)
  end
end
RSpec.describe Account do
  it_behaves_like "auditable" do
    let(:auditable) { Account.new }
  end
end

重载

  • #shared_examples(name, &block) ⇒void

    参数

    • name (String, Symbol, Module)

      在查找此共享组时使用的标识符

    • block

      要评估的块

  • #shared_examples(name, metadata, &block) ⇒void

    参数

    • name (String, Symbol, Module)

      在查找此共享组时使用的标识符

    • metadata (Array<Symbol>, Hash)

      要附加到此组的元数据;任何具有匹配元数据的示例组或示例将自动包含此共享示例组。

    • block

      要评估的块

另请参阅

90
91
92
93
94
95
96
97
98
99
# File 'lib/rspec/core/shared_example_group.rb', line 90
def shared_examples(name, *args, &block)
  top_level = self == ExampleGroup
  if top_level && RSpec::Support.thread_local_data[:in_example_group]
    raise "Creating isolated shared examples from within a context is " \
          "not allowed. Remove `RSpec.` prefix or move this to a " \
          "top-level scope."
  end
  RSpec.world.shared_example_group_registry.add(self, name, *args, &block)
end