lib/mocha/api.rb
def stub_everything(*arguments)
      if Mocha.configuration.reinstate_undocumented_behaviour_from_v1_9?
        if arguments.first.is_a?(Symbol)
          method_name = arguments[0]
          Deprecation.warning(
            "Explicitly include `#{method_name}` in Hash of stubbed methods vs return values,",
            " e.g. `stub_everything(:#{method_name} => nil)`."
          )
          if arguments[1]
            Deprecation.warning(
              "In this case the 2nd argument for `stub_everything(:##{method_name}, ...)` is ignored,",
              ' but in the future a Hash of stubbed methods vs return values will be respected.'
            )
          end
        elsif arguments.first.is_a?(String)
          name = arguments.shift
        end
      elsif arguments.first.is_a?(String) || arguments.first.is_a?(Symbol)
        name = arguments.shift
      end
      expectations = arguments.shift || {}
      stub = name ? Mockery.instance.named_mock(name) : Mockery.instance.unnamed_mock
      stub.stub_everything
      stub.stubs(expectations)
      stub
    end
# Builds a mock object that accepts calls to any method. By default it will return +nil+ for any method call.
#
# @return [Mock] a new mock object
#
# @overload def stub_everything(name)
#   @param [String, Symbol] name identifies mock object in error messages.
#   @note Prior to v1.10.0 when +name+ was a +Symbol+, this method returned an unnamed +Mock+ that stubbed the method identified by +name+. This was undocumented behaviour and it will be removed in the future, but for the moment it can be reinstated using {Configuration#reinstate_undocumented_behaviour_from_v1_9=}.
# @overload def stub_everything(stubbed_methods_vs_return_values = {})
#   @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {Mock#stubs} were called multiple times.
# @overload def stub_everything(name, stubbed_methods_vs_return_values = {})
#   @param [String, Symbol] name identifies mock object in error messages.
#   @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {Mock#stubs} were called multiple times.
#
# @example Ignore invocations of irrelevant methods.
#   def test_motor_stops
#     motor = stub_everything('motor', :stop => true)
#     assert_nil motor.irrelevant_method_1 # => no error raised
#     assert_nil motor.irrelevant_method_2 # => no error raised
#     assert motor.stop
#   end
# rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
Builds a mock object that accepts calls to any method. By default it will return +nil+ for any method call.
Builds a mock object that accepts calls to any method . By default it will return + nil + for any method call .
def stub_everything(*arguments)
      if Mocha.configuration.reinstate_undocumented_behaviour_from_v1_9?
        if arguments.first.is_a?(Symbol)
          method_name = arguments[0]
          Deprecation.warning(
            "Explicitly include `#{method_name}` in Hash of stubbed methods vs return values,",
            " e.g. `stub_everything(:#{method_name} => nil)`."
          )
          if arguments[1]
            Deprecation.warning(
              "In this case the 2nd argument for `stub_everything(:##{method_name}, ...)` is ignored,",
              ' but in the future a Hash of stubbed methods vs return values will be respected.'
            )
          end
        elsif arguments.first.is_a?(String)
          name = arguments.shift
        end
      elsif arguments.first.is_a?(String) || arguments.first.is_a?(Symbol)
        name = arguments.shift
      end
      expectations = arguments.shift || {}
      stub = name ? Mockery.instance.named_mock(name) : Mockery.instance.unnamed_mock
      stub.stub_everything
      stub.stubs(expectations)
      stub
    end
def stub_everything ( * arguments ) if Mocha . configuration . reinstate_undocumented_behaviour_from_v1_9? if arguments . first . is_a? ( Symbol ) method_name = arguments [ 0 ] Deprecation . warning ( "Explicitly include `#{method_name}` in Hash of stubbed methods vs return values," , " e.g. `stub_everything(:#{method_name} => nil)`." ) if arguments [ 1 ] Deprecation . warning ( "In this case the 2nd argument for `stub_everything(:##{method_name}, ...)` is ignored," , ' but in the future a Hash of stubbed methods vs return values will be respected.' ) end elsif arguments . first . is_a? ( String ) name = arguments . shift end elsif arguments . first . is_a? ( String ) || arguments . first . is_a? ( Symbol ) name = arguments . shift end expectations = arguments . shift || { } stub = name ? Mockery . instance . named_mock ( name ) : Mockery . instance . unnamed_mock stub . stub_everything stub . stubs ( expectations ) stub end
Builds a mock object that accepts calls to any method.
Builds a mock object that accepts calls to any method .
a new mock object
a new mock object
Mock
arguments
overload
def stub_everything(name) @param [String, Symbol] name identifies mock object in error messages. @note Prior to v1.10.0 when +name+ was a +Symbol+, this method returned an unnamed +Mock+ that stubbed the method identified by +name+. This was undocumented behaviour and it will be removed in the future, but for the moment it can be reinstated using {Configuration#reinstate_undocumented_behaviour_from_v1_9=}.
def stub_everything ( name ) @param [ String Symbol ] name identifies mock object in error messages . @note Prior to v1 . 10 . 0 when + name + was a + Symbol + this method returned an unnamed + Mock + that stubbed the method identified by + name + . This was undocumented behaviour and it will be removed in the future but for the moment it can be reinstated using { Configuration#reinstate_undocumented_behaviour_from_v1_9 = } .
overload
def stub_everything(stubbed_methods_vs_return_values = {}) @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {Mock#stubs} were called multiple times.
def stub_everything ( stubbed_methods_vs_return_values = {} ) @param [ Hash ] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if { Mock#stubs } were called multiple times .
overload
def stub_everything(name, stubbed_methods_vs_return_values = {}) @param [String, Symbol] name identifies mock object in error messages. @param [Hash] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if {Mock#stubs} were called multiple times.
def stub_everything ( name stubbed_methods_vs_return_values = {} ) @param [ String Symbol ] name identifies mock object in error messages . @param [ Hash ] stubbed_methods_vs_return_values stubbed method name symbols as keys and corresponding return values as values - these stubbed methods are setup as if { Mock#stubs } were called multiple times .
example
Ignore invocations of irrelevant methods. def test_motor_stops motor = stub_everything('motor', :stop => true) assert_nil motor.irrelevant_method_1 # => no error raised assert_nil motor.irrelevant_method_2 # => no error raised assert motor.stop end rubocop:disable Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
Ignore invocations of irrelevant methods . def test_motor_stops motor = stub_everything ( ' motor ' : stop = > true ) assert_nil motor . irrelevant_method_1 # = > no error raised assert_nil motor . irrelevant_method_2 # = > no error raised assert motor . stop end rubocop : disable Metrics / CyclomaticComplexity Metrics / PerceivedComplexity