Module: RSpec::Mocks::ExampleMethods

Includes:
ArgumentMatchers
Defined in:
lib/rspec/mocks/example_methods.rb

Instance Method Summary collapse

Methods included from ArgumentMatchers

#any_args, #anything, #boolean, #duck_type, #hash_excluding, #hash_including, #instance_of, #kind_of, #no_args

Instance Method Details

#allow_message_expectations_on_nilObject

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.



40
41
42
# File 'lib/rspec/mocks/example_methods.rb', line 40

def allow_message_expectations_on_nil
  Proxy.allow_message_expectations_on_nil
end

#double(*args) ⇒ Object

Creates an instance of RSpec::Mocks::Mock.

name is used for failure reporting, so you should use the role that the mock is playing in the example.

Use stubs to declare one or more method stubs in one statement.

Examples:


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"


21
22
23
# File 'lib/rspec/mocks/example_methods.rb', line 21

def double(*args)
  declare_double('Double', *args)
end

#mock(*args) ⇒ Object

Just like double



26
27
28
# File 'lib/rspec/mocks/example_methods.rb', line 26

def mock(*args)
  declare_double('Mock', *args)
end

#stub(*args) ⇒ Object

Just like double



31
32
33
# File 'lib/rspec/mocks/example_methods.rb', line 31

def stub(*args)
  declare_double('Stub', *args)
end

#stub_const(constant_name, value, options = {}) ⇒ Object

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.

Examples:


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

Parameters:

  • constant_name (String)

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

  • value (Object)

    The value to make the constant refer to. When the example completes, the constant will be restored to its prior state.

  • options (Hash) (defaults to: {})

    Stubbing options.

Options Hash (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).

Returns:

  • (Object)

    the stubbed value of the constant



81
82
83
# File 'lib/rspec/mocks/example_methods.rb', line 81

def stub_const(constant_name, value, options = {})
  ConstantStubber.stub(constant_name, value, options)
end