类: RSpec::Mocks::Configuration

继承
Object
  • Object
显示所有
定义在
lib/rspec/mocks/configuration.rb

概述

提供 rspec-mocks 的配置选项。

实例属性摘要 收起

实例方法摘要 收起

构造函数详细信息

#initializeConfiguration

返回 Configuration 的新实例。

5
6
7
8
9
10
11
12
13
# File 'lib/rspec/mocks/configuration.rb', line 5
def initialize
  @allow_message_expectations_on_nil = nil
  @yield_receiver_to_any_instance_implementation_blocks = true
  @verify_doubled_constant_names = false
  @transfer_nested_constants = false
  @verify_partial_doubles = false
  @temporarily_suppress_partial_double_verification = false
  @color = false
end

实例属性详细信息

#allow_message_expectations_on_nilObject

设置 RSpec 在对 nil 设置期望时是否发出警告、忽略或使测试失败。默认情况下,当未设置此标志时,如果对 nil 设置期望,就会发出警告信息。这是为了防止误报,并在早期发现潜在的错误。如果设置为 true,则会抑制警告消息。如果设置为 false,则会抛出错误。

示例

RSpec.configure do |config|
  config.mock_with :rspec do |mocks|
    mocks.allow_message_expectations_on_nil = false
  end
end
29
30
31
# File 'lib/rspec/mocks/configuration.rb', line 29
def allow_message_expectations_on_nil
  @allow_message_expectations_on_nil
end

#color=(value) ⇒Object (只写)

指示 diff 是否应该有颜色。如果加载了 rspec-core,则委托给 rspec-core 的 color 选项;否则,你可以在这里设置它。

171
172
173
# File 'lib/rspec/mocks/configuration.rb', line 171
def color=(value)
  @color = value
end

#transfer_nested_constants=(value) ⇒Object (只写)

设置在存根常量时,transfer_nested_constants 选项的默认值。

145
146
147
# File 'lib/rspec/mocks/configuration.rb', line 145
def transfer_nested_constants=(value)
  @transfer_nested_constants = value
end

#verify_doubled_constant_names=(value) ⇒Object (只写)

当设置为 true 时,如果使用 instance_doubleclass_double 时,传递给它们的名字是一个未定义的常量,就会抛出错误。你可能只想在运行整个测试套件时(包括所有生产代码)设置此选项。如果为隔离的单元测试设置此选项,将阻止你隔离它!

120
121
122
# File 'lib/rspec/mocks/configuration.rb', line 120
def verify_doubled_constant_names=(value)
  @verify_doubled_constant_names = value
end

#yield_receiver_to_any_instance_implementation_blocks=(value) ⇒Object (只写)

设置 RSpec 是否将消息的接收实例传递给用于 any_instance 存根实现的块。如果设置了此选项,第一个传递给块的参数将是接收实例。默认为 true

示例

RSpec.configure do |rspec|
  rspec.mock_with :rspec do |mocks|
    mocks.yield_receiver_to_any_instance_implementation_blocks = false
  end
end
46
47
48
# File 'lib/rspec/mocks/configuration.rb', line 46
def yield_receiver_to_any_instance_implementation_blocks=(value)
  @yield_receiver_to_any_instance_implementation_blocks = value
end

实例方法详细信息

#add_stub_and_should_receive_to(*modules) ⇒Object

stubshould_receive 添加到给定的模块或类中。这通常只在你的应用程序使用了一些代理类时才需要,这些代理类会“简化自身”,只保留最少的几个方法,并且在这个过程中删除了 stubshould_receive

示例

RSpec.configure do |rspec|
  rspec.mock_with :rspec do |mocks|
    mocks.add_stub_and_should_receive_to Delegator
  end
end
62
63
64
65
66
# File 'lib/rspec/mocks/configuration.rb', line 62
def add_stub_and_should_receive_to(*modules)
  modules.each do |mod|
    Syntax.enable_should(mod)
  end
end

#before_verifying_doubles(&block) ⇒Object 又名: when_declaring_verifying_double

提供一种在验证 doubles 时执行自定义操作的方法。

示例

RSpec::Mocks.configuration.before_verifying_doubles do |ref|
  ref.some_method!
end
128
129
130
# File 'lib/rspec/mocks/configuration.rb', line 128
def before_verifying_doubles(&block)
  verifying_double_callbacks << block
end

#color?Boolean

指示 diff 是否应该有颜色。如果加载了 rspec-core,则委托给 rspec-core 的 color 选项;否则,你可以在这里设置它。

返回

  • (Boolean)
176
177
178
# File 'lib/rspec/mocks/configuration.rb', line 176
def color?
  ::RSpec.configuration.color_enabled?
end

#patch_marshal_to_support_partial_doubles=(val) ⇒Object

修改 Marshal.dump 以支持模拟或存根对象的转储。默认情况下,这将不起作用,因为 RSpec 模拟通过添加不能序列化的方法来实现。此修改在序列化之前会删除这些单例方法。设置为假值将删除此修改。

此方法是幂等的。

188
189
190
191
192
193
194
# File 'lib/rspec/mocks/configuration.rb', line 188
def patch_marshal_to_support_partial_doubles=(val)
  if val
    RSpec::Mocks::MarshalExtension.patch!
  else
    RSpec::Mocks::MarshalExtension.unpatch!
  end
end

#reset_syntaxes_to_defaultObject

此方法是私有 API 的一部分。 如果你可能的话,请避免使用此方法,因为它将来可能会被删除或更改。

将配置的语法重置为默认值。

198
199
200
201
# File 'lib/rspec/mocks/configuration.rb', line 198
def reset_syntaxes_to_default
  self.syntax = [:should, :expect]
  RSpec::Mocks::Syntax.warn_about_should!
end

#syntaxObject

返回一个包含已启用的语法列表的数组。

示例

unless RSpec::Mocks.configuration.syntax.include?(:expect)
  raise "this RSpec extension gem requires the rspec-mocks `:expect` syntax"
end
104
105
106
107
108
109
# File 'lib/rspec/mocks/configuration.rb', line 104
def syntax
  syntaxes = []
  syntaxes << :should  if Syntax.should_enabled?
  syntaxes << :expect if Syntax.expect_enabled?
  syntaxes
end

#syntax=(*values) ⇒Object

提供设置 expectshould 或两者语法的功能。RSpec 默认使用 expect 语法。如果你想显式启用 should 语法,以及/或者显式禁用 expect 语法,则需要使用此方法。

end

示例

RSpec.configure do |rspec|
  rspec.mock_with :rspec do |mocks|
    mocks.syntax = [:expect, :should]
  end
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/rspec/mocks/configuration.rb', line 81
def syntax=(*values)
  syntaxes = values.flatten
  if syntaxes.include?(:expect)
    Syntax.enable_expect
  else
    Syntax.disable_expect
  end
  if syntaxes.include?(:should)
    Syntax.enable_should
  else
    Syntax.disable_should
  end
end

#transfer_nested_constants?Boolean

返回

  • (Boolean)
139
140
141
# File 'lib/rspec/mocks/configuration.rb', line 139
def transfer_nested_constants?
  !!@transfer_nested_constants
end

#verify_doubled_constant_names?Boolean

返回

  • (Boolean)
111
112
113
# File 'lib/rspec/mocks/configuration.rb', line 111
def verify_doubled_constant_names?
  !!@verify_doubled_constant_names
end

#verify_partial_doubles=(val) ⇒Object

当设置为 true 时,部分模拟将与对象 doubles 一样进行验证。任何存根都会根据原始方法检查它们的 arguments,并且不存在的方法不能被存根。

150
151
152
# File 'lib/rspec/mocks/configuration.rb', line 150
def verify_partial_doubles=(val)
  @verify_partial_doubles = !!val
end

#verify_partial_doubles?Boolean

返回

  • (Boolean)
154
155
156
# File 'lib/rspec/mocks/configuration.rb', line 154
def verify_partial_doubles?
  @verify_partial_doubles
end

#verifying_double_callbacksObject

此方法是私有 API 的一部分。 如果你可能的话,请避免使用此方法,因为它将来可能会被删除或更改。

返回在验证 doubles 时要调用的块的数组

135
136
137
# File 'lib/rspec/mocks/configuration.rb', line 135
def verifying_double_callbacks
  @verifying_double_callbacks ||= []
end

#yield_receiver_to_any_instance_implementation_blocks?Boolean

返回

  • (Boolean)
31
32
33
# File 'lib/rspec/mocks/configuration.rb', line 31
def yield_receiver_to_any_instance_implementation_blocks?
  @yield_receiver_to_any_instance_implementation_blocks
end