模块:RSpec::Core::DSL

定义在
lib/rspec/core/dsl.rb

概述

DSL 定义了用于分组示例的方法,最著名的是 describe,并将它们作为 RSpec 的类方法公开。它们也可以通过 Configuration 选项 expose_dsl_globally 在全局范围内公开(在 mainModule 的实例上)。

默认情况下,describecontextexample_group 方法会被公开。这些方法为一个或多个示例定义了一个命名上下文。给定的代码块将在生成的 ExampleGroup 子类的上下文中进行评估。

示例

RSpec.describe "something" do
  context "when something is a certain way" do
    it "does something" do
      # example code goes here
    end
  end
end

另请参阅

类方法摘要 收起

类方法详情

.expose_globally!void

此方法属于私有 API 的一部分。 应尽量避免使用此方法,因为它可能会在将来被移除或更改。

将 describe 方法添加到 Module 和顶级绑定中。

58
59
60
61
62
63
64
65
66
# File 'lib/rspec/core/dsl.rb', line 58
def self.expose_globally!
  return if exposed_globally?
  example_group_aliases.each do |method_name|
    expose_example_group_alias_globally(method_name)
  end
  @exposed_globally = true
end

.remove_globally!void

此方法属于私有 API 的一部分。 应尽量避免使用此方法,因为它可能会在将来被移除或更改。

从 Module 和顶级绑定中删除 describe 方法。

70
71
72
73
74
75
76
77
78
# File 'lib/rspec/core/dsl.rb', line 70
def self.remove_globally!
  return unless exposed_globally?
  example_group_aliases.each do |method_name|
    change_global_dsl { undef_method method_name }
  end
  @exposed_globally = false
end