类: RSpec::Expectations::Configuration

继承
Object
  • Object
显示全部
定义于
lib/rspec/expectations/configuration.rb

概述

提供 rspec-expectations 的配置选项。如果您正在使用 rspec-core,您可以通过传递给 RSpec::Core::Configuration#expect_with 的代码块访问它。否则,您可以通过 RSpec::Expectations.configuration 访问它。

示例

RSpec.configure do |rspec|
  rspec.expect_with :rspec do |c|
    # c is the config object
  end
end
# or

RSpec::Expectations.configuration

常量摘要 折叠

NullBacktraceFormatter =

此常量是私有 API 的一部分。 应尽量避免使用此常量,因为它将来可能被移除或更改。

当未加载 rspec-core 时,默认情况下使用的回溯格式化程序的空实现。不进行任何过滤。

Module.new do
  def self.format_backtrace(backtrace)
    backtrace
  end
end

实例属性摘要 折叠

实例方法摘要 折叠

构造函数详细信息

#initializeConfiguration

返回 Configuration 的新实例。

29
30
31
32
# File 'lib/rspec/expectations/configuration.rb', line 29
def initialize
  @on_potential_false_positives = :warn
  @strict_predicate_matchers = false
end

实例属性详细信息

#backtrace_formatterObject

设置或获取回溯格式化程序。回溯格式化程序应实现 #format_backtrace(Array<String>)。这用于格式化由 raise_error 匹配器处理的错误的回溯。

如果您正在使用 rspec-core,将使用 rspec-core 的回溯格式化(包括尊重 --backtrace 选项的存在与否)。

128
# File 'lib/rspec/expectations/configuration.rb', line 128
attr_writer :backtrace_formatter

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

指示是否应为差异着色。如果加载了 rspec-core,则委托给 rspec-core 的颜色选项;否则,您可以在此处设置它。

95
96
97
# File 'lib/rspec/expectations/configuration.rb', line 95
def color=(value)
  @color = value
end

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

设置自定义匹配器描述和失败消息是否应包含使用 chain 定义的方法中的子句。

参数

  • value (Boolean)
140
141
142
# File 'lib/rspec/expectations/configuration.rb', line 140
def include_chain_clauses_in_custom_matcher_descriptions=(value)
  @include_chain_clauses_in_custom_matcher_descriptions = value
end

#on_potential_false_positivesObject

指示 RSpec 如何处理可能导致测试中出现假阳性的匹配器使用情况,通常您希望避免此类情况,因此默认为 true

206
207
208
# File 'lib/rspec/expectations/configuration.rb', line 206
def on_potential_false_positives
  @on_potential_false_positives
end

#strict_predicate_matchersObject

返回属性 strict_predicate_matchers 的值。

197
198
199
# File 'lib/rspec/expectations/configuration.rb', line 197
def strict_predicate_matchers
  @strict_predicate_matchers
end

实例方法详细信息

#add_should_and_should_not_to(*modules) ⇒Object

向给定类或模块添加 shouldshould_not。这可用于确保 should 在诸如代理对象(尤其是 1.8 上的 Delegator 子类对象)之类的事物上正常工作。

参数

  • modules (Array<Module>)

    要向其添加 shouldshould_not 的类或模块列表。

112
113
114
115
116
# File 'lib/rspec/expectations/configuration.rb', line 112
def add_should_and_should_not_to(*modules)
  modules.each do |mod|
    Expectations::Syntax.enable_should(mod)
  end
end

#color?Boolean

指示是否应为差异着色。如果加载了 rspec-core,则委托给 rspec-core 的颜色选项;否则,您可以在此处设置它。

返回

  • (Boolean)
100
101
102
# File 'lib/rspec/expectations/configuration.rb', line 100
def color?
  ::RSpec.configuration.color_enabled?
end

#include_chain_clauses_in_custom_matcher_descriptions?Boolean

指示自定义匹配器描述和失败消息是否应包含使用 chain 定义的方法中的子句。为了向后兼容,它默认情况下为 false。

返回

  • (Boolean)
145
146
147
# File 'lib/rspec/expectations/configuration.rb', line 145
def include_chain_clauses_in_custom_matcher_descriptions?
  @include_chain_clauses_in_custom_matcher_descriptions ||= false
end

#max_formatted_output_length=(length) ⇒Object

配置 RSpec 在格式化对象时将打印的最大字符长度。您可以将 length 设置为 nil 以防止 RSpec 进行截断。

示例

RSpec.configure do |rspec|
  rspec.expect_with :rspec do |c|
    c.max_formatted_output_length = 200
  end
end

参数

  • length (Fixnum)

    将格式化输出限制为的字符数。

70
71
72
# File 'lib/rspec/expectations/configuration.rb', line 70
def max_formatted_output_length=(length)
  RSpec::Support::ObjectFormatter.default_instance.max_formatted_output_length = length
end

#strict_predicate_matchers?Boolean

返回

  • (Boolean)
199
200
201
# File 'lib/rspec/expectations/configuration.rb', line 199
def strict_predicate_matchers?
  @strict_predicate_matchers
end

#syntaxArray<Symbol>

已配置语法的列表。

示例

unless RSpec::Matchers.configuration.syntax.include?(:expect)
  raise "this RSpec extension gem requires the rspec-expectations `:expect` syntax"
end

返回

  • (Array<Symbol>)

    已配置语法的列表。

80
81
82
83
84
85
# File 'lib/rspec/expectations/configuration.rb', line 80
def syntax
  syntaxes = []
  syntaxes << :should if Expectations::Syntax.should_enabled?
  syntaxes << :expect if Expectations::Syntax.expect_enabled?
  syntaxes
end

#syntax=(values) ⇒Object

配置支持的语法。

示例

RSpec.configure do |rspec|
  rspec.expect_with :rspec do |c|
    c.syntax = :should
    # or
    c.syntax = :expect
    # or
    c.syntax = [:should, :expect]
  end
end

参数

  • values (Array<Symbol>, Symbol)

    要启用的语法

46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rspec/expectations/configuration.rb', line 46
def syntax=(values)
  if Array(values).include?(:expect)
    Expectations::Syntax.enable_expect
  else
    Expectations::Syntax.disable_expect
  end
  if Array(values).include?(:should)
    Expectations::Syntax.enable_should
  else
    Expectations::Syntax.disable_should
  end
end

#warn_about_potential_false_positives=(boolean) ⇒Object

配置 RSpec 是否会警告可能导致测试中出现假阳性的匹配器使用情况。

参数

  • boolean (Boolean)
168
169
170
171
172
173
174
175
176
# File 'lib/rspec/expectations/configuration.rb', line 168
def warn_about_potential_false_positives=(boolean)
  if boolean
    self.on_potential_false_positives = :warn
  elsif warn_about_potential_false_positives?
    self.on_potential_false_positives = :nothing
  else
    # no-op, handler is something else
  end
end

#warn_about_potential_false_positives?Boolean

指示 RSpec 是否会警告可能导致测试中出现假阳性的匹配器使用情况,通常您希望避免此类情况,因此默认为 true

返回

  • (Boolean)
211
212
213
# File 'lib/rspec/expectations/configuration.rb', line 211
def warn_about_potential_false_positives?
  on_potential_false_positives == :warn
end