Class: RSpec::Expectations::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec/expectations/configuration.rb

Overview

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.

Examples:

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

# or

RSpec::Expectations.configuration

Constant Summary

NullBacktraceFormatter =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

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

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration



29
30
31
# File 'lib/rspec/expectations/configuration.rb', line 29

def initialize
  @on_potential_false_positives = :warn
end

Instance Attribute Details

#backtrace_formatterObject

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).



113
# File 'lib/rspec/expectations/configuration.rb', line 113

attr_writer :backtrace_formatter

#color=(value) ⇒ Object (writeonly)

Indicates whether or not diffs should be colored. Delegates to rspec-core's color option if rspec-core is loaded; otherwise you can set it here.



80
81
82
# File 'lib/rspec/expectations/configuration.rb', line 80

def color=(value)
  @color = value
end

#include_chain_clauses_in_custom_matcher_descriptions=(value) ⇒ Object (writeonly)

Sets if custom matcher descriptions and failure messages should include clauses from methods defined using chain.

Parameters:

  • value (Boolean)


125
126
127
# File 'lib/rspec/expectations/configuration.rb', line 125

def include_chain_clauses_in_custom_matcher_descriptions=(value)
  @include_chain_clauses_in_custom_matcher_descriptions = value
end

#on_potential_false_positivesObject

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.



177
178
179
# File 'lib/rspec/expectations/configuration.rb', line 177

def on_potential_false_positives
  @on_potential_false_positives
end

Instance Method Details

#add_should_and_should_not_to(*modules) ⇒ Object

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).

Parameters:

  • modules (Array<Module>)

    the list of classes or modules to add should and should_not to.



97
98
99
100
101
# File 'lib/rspec/expectations/configuration.rb', line 97

def add_should_and_should_not_to(*modules)
  modules.each do |mod|
    Expectations::Syntax.enable_should(mod)
  end
end

#color?Boolean

Indicates whether or not diffs should be colored. Delegates to rspec-core's color option if rspec-core is loaded; otherwise you can set it here.

Returns:

  • (Boolean)


85
86
87
# File 'lib/rspec/expectations/configuration.rb', line 85

def color?
  ::RSpec.configuration.color_enabled?
end

#include_chain_clauses_in_custom_matcher_descriptions?Boolean

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.

Returns:

  • (Boolean)


130
131
132
# File 'lib/rspec/expectations/configuration.rb', line 130

def include_chain_clauses_in_custom_matcher_descriptions?
  @include_chain_clauses_in_custom_matcher_descriptions ||= false
end

#syntaxArray<Symbol>

The list of configured syntaxes.

Examples:

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

Returns:

  • (Array<Symbol>)

    the list of configured syntaxes.



65
66
67
68
69
70
# File 'lib/rspec/expectations/configuration.rb', line 65

def syntax
  syntaxes = []
  syntaxes << :should if Expectations::Syntax.should_enabled?
  syntaxes << :expect if Expectations::Syntax.expect_enabled?
  syntaxes
end

#syntax=(values) ⇒ Object

Configures the supported syntax.

Examples:

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

Parameters:

  • values (Array<Symbol>, Symbol)

    the syntaxes to enable



45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rspec/expectations/configuration.rb', line 45

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

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

Parameters:

  • boolean (Boolean)


153
154
155
156
157
158
159
160
161
# File 'lib/rspec/expectations/configuration.rb', line 153

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

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.

Returns:

  • (Boolean)


182
183
184
# File 'lib/rspec/expectations/configuration.rb', line 182

def warn_about_potential_false_positives?
  on_potential_false_positives == :warn
end