Class: RSpec::Mocks::Configuration

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

Overview

Provides configuration options for rspec-mocks.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration



5
6
7
8
9
10
11
12
# 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
end

Instance Attribute Details

#allow_message_expectations_on_nilObject

Sets whether RSpec will warn, ignore, or fail a test when expectations are set on nil. By default, when this flag is not set, warning messages are issued when expectations are set on nil. This is to prevent false-positives and to catch potential bugs early on. When set to true, warning messages are suppressed. When set to false, it will raise an error.

Examples:

RSpec.configure do |config|
  config.mock_with :rspec do |mocks|
    mocks.allow_message_expectations_on_nil = false
  end
end


28
29
30
# File 'lib/rspec/mocks/configuration.rb', line 28

def allow_message_expectations_on_nil
  @allow_message_expectations_on_nil
end

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



170
171
172
# File 'lib/rspec/mocks/configuration.rb', line 170

def color=(value)
  @color = value
end

#transfer_nested_constants=(value) ⇒ Object (writeonly)

Sets the default for the transfer_nested_constants option when stubbing constants.



144
145
146
# File 'lib/rspec/mocks/configuration.rb', line 144

def transfer_nested_constants=(value)
  @transfer_nested_constants = value
end

#verify_doubled_constant_names=(value) ⇒ Object (writeonly)

When this is set to true, an error will be raised when instance_double or class_double is given the name of an undefined constant. You probably only want to set this when running your entire test suite, with all production code loaded. Setting this for an isolated unit test will prevent you from being able to isolate it!



119
120
121
# File 'lib/rspec/mocks/configuration.rb', line 119

def verify_doubled_constant_names=(value)
  @verify_doubled_constant_names = value
end

#yield_receiver_to_any_instance_implementation_blocks=(value) ⇒ Object (writeonly)

Sets whether or not RSpec will yield the receiving instance of a message to blocks that are used for any_instance stub implementations. When set, the first yielded argument will be the receiving instance. Defaults to true.

Examples:

RSpec.configure do |rspec|
  rspec.mock_with :rspec do |mocks|
    mocks.yield_receiver_to_any_instance_implementation_blocks = false
  end
end


45
46
47
# File 'lib/rspec/mocks/configuration.rb', line 45

def yield_receiver_to_any_instance_implementation_blocks=(value)
  @yield_receiver_to_any_instance_implementation_blocks = value
end

Instance Method Details

#add_stub_and_should_receive_to(*modules) ⇒ Object

Adds stub and should_receive to the given modules or classes. This is usually only necessary if you application uses some proxy classes that "strip themselves down" to a bare minimum set of methods and remove stub and should_receive in the process.

Examples:

RSpec.configure do |rspec|
  rspec.mock_with :rspec do |mocks|
    mocks.add_stub_and_should_receive_to Delegator
  end
end


61
62
63
64
65
# File 'lib/rspec/mocks/configuration.rb', line 61

def add_stub_and_should_receive_to(*modules)
  modules.each do |mod|
    Syntax.enable_should(mod)
  end
end

#before_verifying_doubles(&block) ⇒ Object Also known as: when_declaring_verifying_double

Provides a way to perform customisations when verifying doubles.

Examples:

RSpec::Mocks.configuration.before_verifying_doubles do |ref|
  ref.some_method!
end


127
128
129
# File 'lib/rspec/mocks/configuration.rb', line 127

def before_verifying_doubles(&block)
  verifying_double_callbacks << block
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)


175
176
177
# File 'lib/rspec/mocks/configuration.rb', line 175

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

#patch_marshal_to_support_partial_doubles=(val) ⇒ Object

Monkey-patch Marshal.dump to enable dumping of mocked or stubbed objects. By default this will not work since RSpec mocks works by adding singleton methods that cannot be serialized. This patch removes these singleton methods before serialization. Setting to falsey removes the patch.

This method is idempotent.



187
188
189
190
191
192
193
# File 'lib/rspec/mocks/configuration.rb', line 187

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

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

Resets the configured syntax to the default.



197
198
199
200
# File 'lib/rspec/mocks/configuration.rb', line 197

def reset_syntaxes_to_default
  self.syntax = [:should, :expect]
  RSpec::Mocks::Syntax.warn_about_should!
end

#syntaxObject

Returns an array with a list of syntaxes that are enabled.

Examples:

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


103
104
105
106
107
108
# File 'lib/rspec/mocks/configuration.rb', line 103

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

#syntax=(*values) ⇒ Object

Provides the ability to set either expect, should or both syntaxes. RSpec uses expect syntax by default. This is needed if you want to explicitly enable should syntax and/or explicitly disable expect syntax.

end

Examples:

RSpec.configure do |rspec|
  rspec.mock_with :rspec do |mocks|
    mocks.syntax = [:expect, :should]
  end


80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rspec/mocks/configuration.rb', line 80

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

Returns:

  • (Boolean)


138
139
140
# File 'lib/rspec/mocks/configuration.rb', line 138

def transfer_nested_constants?
  !!@transfer_nested_constants
end

#verify_doubled_constant_names?Boolean

Returns:

  • (Boolean)


110
111
112
# File 'lib/rspec/mocks/configuration.rb', line 110

def verify_doubled_constant_names?
  !!@verify_doubled_constant_names
end

#verify_partial_doubles=(val) ⇒ Object

When set to true, partial mocks will be verified the same as object doubles. Any stubs will have their arguments checked against the original method, and methods that do not exist cannot be stubbed.



149
150
151
# File 'lib/rspec/mocks/configuration.rb', line 149

def verify_partial_doubles=(val)
  @verify_partial_doubles = !!val
end

#verify_partial_doubles?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/rspec/mocks/configuration.rb', line 153

def verify_partial_doubles?
  @verify_partial_doubles
end

#verifying_double_callbacksObject

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

Returns an array of blocks to call when verifying doubles



134
135
136
# File 'lib/rspec/mocks/configuration.rb', line 134

def verifying_double_callbacks
  @verifying_double_callbacks ||= []
end

#yield_receiver_to_any_instance_implementation_blocks?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/rspec/mocks/configuration.rb', line 30

def yield_receiver_to_any_instance_implementation_blocks?
  @yield_receiver_to_any_instance_implementation_blocks
end