class RSpec::Expectations::Configuration

Provides configuration options for rspec-expectations. If you are using rspec-core, you can access this via a block passed to ‘RSpec::Core::Configuration#expect_with`. Otherwise, you can access it via RSpec::Expectations.configuration.

@example

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

# or

RSpec::Expectations.configuration

Constants

FALSE_POSITIVE_BEHAVIOURS

@private

NullBacktraceFormatter

@api private Null implementation of a backtrace formatter used by default when rspec-core is not loaded. Does no filtering.

Attributes

backtrace_formatter[W]

Sets or gets the backtrace formatter. The backtrace formatter should implement ‘#format_backtrace(Array<String>)`. This is used to format backtraces of errors handled by the `raise_error` matcher.

If you are using rspec-core, rspec-core’s backtrace formatting will be used (including respecting the presence or absence of the ‘–backtrace` option).

@!attribute [rw] backtrace_formatter

include_chain_clauses_in_custom_matcher_descriptions[W]

Sets if custom matcher descriptions and failure messages should include clauses from methods defined using ‘chain`. @param value [Boolean]

on_potential_false_positives[R]

Indicates what RSpec will do about matcher use which will potentially cause false positives in tests, generally you want to avoid such scenarios so this defaults to ‘true`.

strict_predicate_matchers[R]

Public Class Methods

new() click to toggle source
# File lib/rspec/expectations/configuration.rb, line 29
def initialize
  @on_potential_false_positives = :warn
  @strict_predicate_matchers = false
end

Public Instance Methods

add_should_and_should_not_to(*modules) click to toggle source

:nocov: Because this is only really useful on 1.8, and hard to test elsewhere.

Adds ‘should` and `should_not` to the given classes or modules. This can be used to ensure `should` works properly on things like proxy objects (particular `Delegator`-subclassed objects on 1.8).

@param [Array<Module>] modules the list of classes or modules

to add `should` and `should_not` to.
# File lib/rspec/expectations/configuration.rb, line 116
def add_should_and_should_not_to(*modules)
  modules.each do |mod|
    Expectations::Syntax.enable_should(mod)
  end
end
backtrace_formatter() click to toggle source
# File lib/rspec/expectations/configuration.rb, line 134
def backtrace_formatter
  @backtrace_formatter ||= if defined?(::RSpec.configuration.backtrace_formatter)
                             ::RSpec.configuration.backtrace_formatter
                           else
                             NullBacktraceFormatter
                           end
end
color?() click to toggle source
# File lib/rspec/expectations/configuration.rb, line 88
def color?
  ::RSpec.configuration.color_enabled?
end
false_positives_handler() click to toggle source

@private

# File lib/rspec/expectations/configuration.rb, line 221
def false_positives_handler
  FALSE_POSITIVE_BEHAVIOURS.fetch(@on_potential_false_positives)
end
include_chain_clauses_in_custom_matcher_descriptions?() click to toggle source

Indicates whether or not custom matcher descriptions and failure messages should include clauses from methods defined using ‘chain`. It is false by default for backwards compatibility.

# File lib/rspec/expectations/configuration.rb, line 150
def include_chain_clauses_in_custom_matcher_descriptions?
  @include_chain_clauses_in_custom_matcher_descriptions ||= false
end
max_formatted_output_length=(length) click to toggle source

Configures the maximum character length that RSpec will print while formatting an object. You can set length to nil to prevent RSpec from doing truncation. @param [Fixnum] length the number of characters to limit the formatted output to. @example

RSpec.configure do |rspec|
  rspec.expect_with :rspec do |c|
    c.max_formatted_output_length = 200
  end
end
# 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
on_potential_false_positives=(behavior) click to toggle source

Configures what RSpec will do about matcher use which will potentially cause false positives in tests.

@param [Symbol] behavior can be set to :warn, :raise or :nothing

# File lib/rspec/expectations/configuration.rb, line 187
def on_potential_false_positives=(behavior)
  unless FALSE_POSITIVE_BEHAVIOURS.key?(behavior)
    raise ArgumentError, "Supported values are: #{FALSE_POSITIVE_BEHAVIOURS.keys}"
  end
  @on_potential_false_positives = behavior
end
reset_syntaxes_to_default() click to toggle source

@private

# File lib/rspec/expectations/configuration.rb, line 155
def reset_syntaxes_to_default
  self.syntax = [:should, :expect]
  RSpec::Expectations::Syntax.warn_about_should!
end
strict_predicate_matchers=(flag) click to toggle source

Configures RSpec to check predicate matchers to ‘be(true)` / `be(false)` (strict), or `be_truthy` / `be_falsey` (not strict). Historically, the default was `false`, but `true` is recommended.

# File lib/rspec/expectations/configuration.rb, line 197
def strict_predicate_matchers=(flag)
  raise ArgumentError, "Pass `true` or `false`" unless flag == true || flag == false
  @strict_predicate_matchers = flag
end
strict_predicate_matchers?() click to toggle source
# File lib/rspec/expectations/configuration.rb, line 204
def strict_predicate_matchers?
  @strict_predicate_matchers
end
syntax() click to toggle source

The list of configured syntaxes. @return [Array<Symbol>] the list of configured syntaxes. @example

unless RSpec::Matchers.configuration.syntax.include?(:expect)
  raise "this RSpec extension gem requires the rspec-expectations `:expect` syntax"
end
# 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) click to toggle source

Configures the supported syntax. @param [Array<Symbol>, Symbol] values the syntaxes to enable @example

RSpec.configure do |rspec|
  rspec.expect_with :rspec do |c|
    c.syntax = :should
    # or
    c.syntax = :expect
    # or
    c.syntax = [:should, :expect]
  end
end
# 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) click to toggle source

Configures whether RSpec will warn about matcher use which will potentially cause false positives in tests.

@param [Boolean] boolean

# File lib/rspec/expectations/configuration.rb, line 173
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?() click to toggle source

Indicates whether RSpec will warn about matcher use which will potentially cause false positives in tests, generally you want to avoid such scenarios so this defaults to ‘true`.

# File lib/rspec/expectations/configuration.rb, line 216
def warn_about_potential_false_positives?
  on_potential_false_positives == :warn
end