Module: RSpec::Mocks::ExampleMethods

### Home Classes Methods

### In Files

- lib/rspec/mocks/example_methods.rb

### Namespace

- MODULE RSpec::Mocks::ExampleMethods::ExpectHost

### Methods

- ::declare_double
- ::declare_verifying_double
- ::extended
- ::included
- #allow_message_expectations_on_nil
- #class_double
- #class_spy
- #double
- #have_received
- #hide_const
- #instance_double
- #instance_spy
- #object_double
- #object_spy
- #spy
- #stub_const

### Included Modules

- RSpec::Mocks::ArgumentMatchers
- RSpec::Mocks::ExampleMethods::ExpectHost

### Class/Module Index

Quicksearch

- RSpec
- RSpec::Mocks
- RSpec::Mocks::AllowanceTarget
- RSpec::Mocks::AndReturnImplementation
- RSpec::Mocks::AndWrapOriginalImplementation
- RSpec::Mocks::AndYieldImplementation
- RSpec::Mocks::AnyInstance
- RSpec::Mocks::AnyInstance::Chain
- RSpec::Mocks::AnyInstance::Chain::Customizations
- RSpec::Mocks::AnyInstance::ErrorGenerator
- RSpec::Mocks::AnyInstance::ExpectChainChain
- RSpec::Mocks::AnyInstance::ExpectationChain
- RSpec::Mocks::AnyInstance::FluentInterfaceProxy
- RSpec::Mocks::AnyInstance::MessageChains
- RSpec::Mocks::AnyInstance::PositiveExpectationChain
- RSpec::Mocks::AnyInstance::Proxy
- RSpec::Mocks::AnyInstance::Recorder
- RSpec::Mocks::AnyInstance::StubChain
- RSpec::Mocks::AnyInstance::StubChainChain
- RSpec::Mocks::AnyInstanceAllowanceTarget
- RSpec::Mocks::AnyInstanceExpectationTarget
- RSpec::Mocks::ArgumentListMatcher
- RSpec::Mocks::ArgumentMatchers
- RSpec::Mocks::ArgumentMatchers::AnyArgMatcher
- RSpec::Mocks::ArgumentMatchers::AnyArgsMatcher
- RSpec::Mocks::ArgumentMatchers::ArrayIncludingMatcher
- RSpec::Mocks::ArgumentMatchers::BaseHashMatcher
- RSpec::Mocks::ArgumentMatchers::BooleanMatcher
- RSpec::Mocks::ArgumentMatchers::DuckTypeMatcher
- RSpec::Mocks::ArgumentMatchers::HashExcludingMatcher
- RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher
- RSpec::Mocks::ArgumentMatchers::InstanceOf
- RSpec::Mocks::ArgumentMatchers::KindOf
- RSpec::Mocks::ArgumentMatchers::NoArgsMatcher
- RSpec::Mocks::ArgumentMatchers::SingletonMatcher
- RSpec::Mocks::CallbackInvocationStrategy
- RSpec::Mocks::ClassNewMethodReference
- RSpec::Mocks::ClassVerifyingDouble
- RSpec::Mocks::Configuration
- RSpec::Mocks::Constant
- RSpec::Mocks::ConstantMutator
- RSpec::Mocks::ConstantMutator::BaseMutator
- RSpec::Mocks::ConstantMutator::ConstantHider
- RSpec::Mocks::ConstantMutator::DefinedConstantReplacer
- RSpec::Mocks::ConstantMutator::UndefinedConstantSetter
- RSpec::Mocks::DirectObjectReference
- RSpec::Mocks::Double
- RSpec::Mocks::ErrorGenerator
- RSpec::Mocks::ExampleMethods
- RSpec::Mocks::ExampleMethods::ExpectHost
- RSpec::Mocks::ExpectChain
- RSpec::Mocks::ExpectationTarget
- RSpec::Mocks::Implementation
- RSpec::Mocks::InstanceMethodReference
- RSpec::Mocks::InstanceMethodStasher
- RSpec::Mocks::InstanceVerifyingDouble
- RSpec::Mocks::MarshalExtension
- RSpec::Mocks::Matchers
- RSpec::Mocks::Matchers::ExpectationCustomization
- RSpec::Mocks::Matchers::HaveReceived
- RSpec::Mocks::Matchers::Receive
- RSpec::Mocks::Matchers::Receive::DefaultDescribable
- RSpec::Mocks::Matchers::ReceiveMessageChain
- RSpec::Mocks::Matchers::ReceiveMessages
- RSpec::Mocks::MessageChain
- RSpec::Mocks::MessageExpectation
- RSpec::Mocks::MessageExpectation::ImplementationDetails
- RSpec::Mocks::MethodDouble
- RSpec::Mocks::MethodReference
- RSpec::Mocks::Mutex
- RSpec::Mocks::NamedObjectReference
- RSpec::Mocks::NestedSpace
- RSpec::Mocks::NoCallbackInvocationStrategy
- RSpec::Mocks::ObjectMethodReference
- RSpec::Mocks::ObjectReference
- RSpec::Mocks::ObjectVerifyingDouble
- RSpec::Mocks::ObjectVerifyingDoubleMethods
- RSpec::Mocks::OrderGroup
- RSpec::Mocks::PartialClassDoubleProxy
- RSpec::Mocks::PartialClassDoubleProxyMethods
- RSpec::Mocks::PartialDoubleProxy
- RSpec::Mocks::Proxy
- RSpec::Mocks::ProxyForNil
- RSpec::Mocks::ReentrantMutex
- RSpec::Mocks::RootSpace
- RSpec::Mocks::SimpleMessageExpectation
- RSpec::Mocks::Space
- RSpec::Mocks::StubChain
- RSpec::Mocks::Syntax
- RSpec::Mocks::TargetBase
- RSpec::Mocks::TestDouble
- RSpec::Mocks::TestDoubleFormatter
- RSpec::Mocks::TestDoubleProxy
- RSpec::Mocks::VerifyingDouble
- RSpec::Mocks::VerifyingDouble::SilentIO
- RSpec::Mocks::VerifyingExistingClassNewMethodDouble
- RSpec::Mocks::VerifyingExistingMethodDouble
- RSpec::Mocks::VerifyingMessageExpectation
- RSpec::Mocks::VerifyingMethodDouble
- RSpec::Mocks::VerifyingPartialClassDoubleProxy
- RSpec::Mocks::VerifyingPartialDoubleProxy
- RSpec::Mocks::VerifyingProxy
- RSpec::Mocks::VerifyingProxyMethods
- RSpec::Mocks::Version
- BasicObject
- Class

No matching classes.

# RSpec::Mocks::ExampleMethods

Contains methods intended to be used from within code examples. Mix this in to your test context (such as a test framework base class) to use rspec-mocks with your test framework. If you're using rspec-core, it'll take care of doing this for you.

### Public Class Methods

declare_double(type, *args) click to toggle source

@private

    # File lib/rspec/mocks/example_methods.rb, line 410
    def self.declare_double(type, *args)
      args << {} unless Hash === args.last
      type.new(*args)
    end

declare_verifying_double(type, ref, *args) click to toggle source

@private

    # File lib/rspec/mocks/example_methods.rb, line 395
    def self.declare_verifying_double(type, ref, *args)
      if RSpec::Mocks.configuration.verify_doubled_constant_names? &&
        !ref.defined?

        RSpec::Mocks.error_generator.raise_verifying_double_not_defined_error(ref)
      end

      RSpec::Mocks.configuration.verifying_double_callbacks.each do |block|
        block.call(ref)
      end

      declare_double(type, ref, *args)
    end

extended(object) click to toggle source

@private

    # File lib/rspec/mocks/example_methods.rb, line 388
    def self.extended(object)
      # This gets extended in so that if `RSpec::Matchers` is included in
      # `klass` later, it's definition of `expect` will take precedence.
      object.extend ExpectHost unless object.respond_to?(:expect)
    end

included(klass) click to toggle source

@private

    # File lib/rspec/mocks/example_methods.rb, line 379
    def self.included(klass)
      klass.class_exec do
        # This gets mixed in so that if `RSpec::Matchers` is included in
        # `klass` later, it's definition of `expect` will take precedence.
        include ExpectHost unless method_defined?(:expect)
      end
    end

### Public Instance Methods

allow_message_expectations_on_nil() click to toggle source

Disables warning messages about expectations being set on nil.

By default warning messages are issued when expectations are set on nil. This is to prevent false-positives and to catch potential bugs early on.

    # File lib/rspec/mocks/example_methods.rb, line 200
    def allow_message_expectations_on_nil
      RSpec::Mocks.space.proxy_for(nil).warn_about_expectations = false
    end

class_double(doubled_class, *args) click to toggle source

@overload class_double(doubled_class)

    @param doubled_class [String, Module]

@overload class_double(doubled_class, name)

    @param doubled_class [String, Module]
    @param name [String/Symbol] name or description to be used in failure messages

@overload class_double(doubled_class, stubs)

    @param doubled_class [String, Module]
    @param stubs [Hash] hash of message/return-value pairs

@overload class_double(doubled_class, name, stubs)

    @param doubled_class [String, Module]
    @param name [String/Symbol] name or description to be used in failure messages
    @param stubs [Hash] hash of message/return-value pairs

@return ClassVerifyingDouble

Constructs a test double against a specific class. If the given class name has been loaded, only class methods defined on the class are allowed to be stubbed. In all other ways it behaves like a [double](double).

    # File lib/rspec/mocks/example_methods.rb, line 79
    def class_double(doubled_class, *args)
      ref = ObjectReference.for(doubled_class)
      ExampleMethods.declare_verifying_double(ClassVerifyingDouble, ref, *args)
    end

class_spy(*args) click to toggle source

@overload class_spy(doubled_class)

    @param doubled_class [String, Module]

@overload class_spy(doubled_class, name)

    @param doubled_class [String, Class]
    @param name [String/Symbol] name or description to be used in failure messages

@overload class_spy(doubled_class, stubs)

    @param doubled_class [String, Module]
    @param stubs [Hash] hash of message/return-value pairs

@overload class_spy(doubled_class, name, stubs)

    @param doubled_class [String, Class]
    @param name [String/Symbol] name or description to be used in failure messages
    @param stubs [Hash] hash of message/return-value pairs

@return ClassVerifyingDouble

Constructs a test double that is optimized for use with `have_received` against a specific class. If the given class name has been loaded, only class methods defined on the class are allowed to be stubbed. With a normal double one has to stub methods in order to be able to spy them. An class_spy automatically spies on all class methods to which the class responds.

    # File lib/rspec/mocks/example_methods.rb, line 191
    def class_spy(*args)
      class_double(*args).as_null_object
    end

double(*args) click to toggle source

@overload double() @overload double(name)

    @param name [String/Symbol] name or description to be used in failure messages

@overload double(stubs)

    @param stubs (Hash) hash of message/return-value pairs

@overload double(name, stubs)

    @param name [String/Symbol] name or description to be used in failure messages
    @param stubs (Hash) hash of message/return-value pairs

@return (Double)

Constructs an instance of [RSpec::Mocks::Double](RSpec::Mocks::Double) configured with an optional name, used for reporting in failure messages, and an optional hash of message/return-value pairs.

@example

    book = double("book", :title => "The RSpec Book")
    book.title #=> "The RSpec Book"

    card = double("card", :suit => "Spades", :rank => "A")
    card.suit  #=> "Spades"
    card.rank  #=> "A"

    # File lib/rspec/mocks/example_methods.rb, line 34
    def double(*args)
      ExampleMethods.declare_double(Double, *args)
    end

have_received(method_name, &block) click to toggle source

Verifies that the given object received the expected message during the course of the test. On a spy objects or as null object doubles this works for any method, on other objects the method must have been stubbed beforehand in order for messages to be verified.

Stubbing and verifying messages received in this way implements the Test Spy pattern.

@param method_name [Symbol] name of the method expected to have been

    called.

@example

    invitation = double('invitation', accept: true)
    user.accept_invitation(invitation)
    expect(invitation).to have_received(:accept)

    # You can also use most message expectations:
    expect(invitation).to have_received(:accept).with(mailer).once

@note `have_received(...).with(...)` is unable to work properly when

    passed arguments are mutated after the spy records the received message.

    # File lib/rspec/mocks/example_methods.rb, line 280
    def have_received(method_name, &block)
      Matchers::HaveReceived.new(method_name, &block)
    end

hide_const(constant_name) click to toggle source

Hides the named constant with the given value. The constant will be undefined for the duration of the test.

Like method stubs, the constant will be restored to its original value when the example completes.

@param constant_name [String] The fully qualified name of the constant.

    The current constant scoping at the point of call is not considered.

@example

    hide_const("MyClass") # => MyClass is now an undefined constant

    # File lib/rspec/mocks/example_methods.rb, line 255
    def hide_const(constant_name)
      ConstantMutator.hide(constant_name)
    end

instance_double(doubled_class, *args) click to toggle source

@overload instance_double(doubled_class)

    @param doubled_class [String, Class]

@overload instance_double(doubled_class, name)

    @param doubled_class [String, Class]
    @param name [String/Symbol] name or description to be used in failure messages

@overload instance_double(doubled_class, stubs)

    @param doubled_class [String, Class]
    @param stubs [Hash] hash of message/return-value pairs

@overload instance_double(doubled_class, name, stubs)

    @param doubled_class [String, Class]
    @param name [String/Symbol] name or description to be used in failure messages
    @param stubs [Hash] hash of message/return-value pairs

@return InstanceVerifyingDouble

Constructs a test double against a specific class. If the given class name has been loaded, only instance methods defined on the class are allowed to be stubbed. In all other ways it behaves like a [double](double).

    # File lib/rspec/mocks/example_methods.rb, line 56
    def instance_double(doubled_class, *args)
      ref = ObjectReference.for(doubled_class)
      ExampleMethods.declare_verifying_double(InstanceVerifyingDouble, ref, *args)
    end

instance_spy(*args) click to toggle source

@overload instance_spy(doubled_class)

    @param doubled_class [String, Class]

@overload instance_spy(doubled_class, name)

    @param doubled_class [String, Class]
    @param name [String/Symbol] name or description to be used in failure messages

@overload instance_spy(doubled_class, stubs)

    @param doubled_class [String, Class]
    @param stubs [Hash] hash of message/return-value pairs

@overload instance_spy(doubled_class, name, stubs)

    @param doubled_class [String, Class]
    @param name [String/Symbol] name or description to be used in failure messages
    @param stubs [Hash] hash of message/return-value pairs

@return InstanceVerifyingDouble

Constructs a test double that is optimized for use with `have_received` against a specific class. If the given class name has been loaded, only instance methods defined on the class are allowed to be stubbed. With a normal double one has to stub methods in order to be able to spy them. An instance_spy automatically spies on all instance methods to which the class responds.

    # File lib/rspec/mocks/example_methods.rb, line 144
    def instance_spy(*args)
      instance_double(*args).as_null_object
    end

object_double(object_or_name, *args) click to toggle source

@overload object_double(object_or_name)

    @param object_or_name [String, Object]

@overload object_double(object_or_name, name)

    @param object_or_name [String, Object]
    @param name [String/Symbol] name or description to be used in failure messages

@overload object_double(object_or_name, stubs)

    @param object_or_name [String, Object]
    @param stubs [Hash] hash of message/return-value pairs

@overload object_double(object_or_name, name, stubs)

    @param object_or_name [String, Object]
    @param name [String/Symbol] name or description to be used in failure messages
    @param stubs [Hash] hash of message/return-value pairs

@return ObjectVerifyingDouble

Constructs a test double against a specific object. Only the methods the object responds to are allowed to be stubbed. If a String argument is provided, it is assumed to reference a constant object which is used for verification. In all other ways it behaves like a [double](double).

    # File lib/rspec/mocks/example_methods.rb, line 102
    def object_double(object_or_name, *args)
      ref = ObjectReference.for(object_or_name, :allow_direct_object_refs)
      ExampleMethods.declare_verifying_double(ObjectVerifyingDouble, ref, *args)
    end

object_spy(*args) click to toggle source

@overload object_spy(object_or_name)

    @param object_or_name [String, Object]

@overload object_spy(object_or_name, name)

    @param object_or_name [String, Class]
    @param name [String/Symbol] name or description to be used in failure messages

@overload object_spy(object_or_name, stubs)

    @param object_or_name [String, Object]
    @param stubs [Hash] hash of message/return-value pairs

@overload object_spy(object_or_name, name, stubs)

    @param object_or_name [String, Class]
    @param name [String/Symbol] name or description to be used in failure messages
    @param stubs [Hash] hash of message/return-value pairs

@return ObjectVerifyingDouble

Constructs a test double that is optimized for use with `have_received` against a specific object. Only instance methods defined on the object are allowed to be stubbed. With a normal double one has to stub methods in order to be able to spy them. An object_spy automatically spies on all methods to which the object responds.

    # File lib/rspec/mocks/example_methods.rb, line 167
    def object_spy(*args)
      object_double(*args).as_null_object
    end

spy(*args) click to toggle source

@overload spy() @overload spy(name)

    @param name [String/Symbol] name or description to be used in failure messages

@overload spy(stubs)

    @param stubs (Hash) hash of message/return-value pairs

@overload spy(name, stubs)

    @param name [String/Symbol] name or description to be used in failure messages
    @param stubs (Hash) hash of message/return-value pairs

@return (Double)

Constructs a test double that is optimized for use with `have_received`. With a normal double one has to stub methods in order to be able to spy them. A spy automatically spies on all methods.

    # File lib/rspec/mocks/example_methods.rb, line 120
    def spy(*args)
      double(*args).as_null_object
    end

stub_const(constant_name, value, options={}) click to toggle source

Stubs the named constant with the given value. Like method stubs, the constant will be restored to its original value (or lack of one, if it was undefined) when the example completes.

@param constant_name [String] The fully qualified name of the constant. The current

    constant scoping at the point of call is not considered.

@param value [Object] The value to make the constant refer to. When the

    example completes, the constant will be restored to its prior state.

@param options [Hash] Stubbing options. @option options :transfer_nested_constants [Boolean, Array<Symbol>] Determines

    what nested constants, if any, will be transferred from the original value
    of the constant to the new value of the constant. This only works if both
    the original and new values are modules (or classes).

@return [Object] the stubbed value of the constant

@example

    stub_const("MyClass", Class.new) # => Replaces (or defines) MyClass with a new class object.
    stub_const("SomeModel::PER_PAGE", 5) # => Sets SomeModel::PER_PAGE to 5.

    class CardDeck
      SUITS = [:Spades, :Diamonds, :Clubs, :Hearts]
      NUM_CARDS = 52
    end

    stub_const("CardDeck", Class.new)
    CardDeck::SUITS # => uninitialized constant error
    CardDeck::NUM_CARDS # => uninitialized constant error

    stub_const("CardDeck", Class.new, :transfer_nested_constants => true)
    CardDeck::SUITS # => our suits array
    CardDeck::NUM_CARDS # => 52

    stub_const("CardDeck", Class.new, :transfer_nested_constants => [:SUITS])
    CardDeck::SUITS # => our suits array
    CardDeck::NUM_CARDS # => uninitialized constant error

    # File lib/rspec/mocks/example_methods.rb, line 240
    def stub_const(constant_name, value, options={})
      ConstantMutator.stub(constant_name, value, options)
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.