Module: RSpec::SpyOn::ExampleMethods

Defined in:
lib/rspec/spy_on/example_methods.rb

Instance Method Summary collapse

Instance Method Details

#spy_on(target, *messages) ⇒ target

Enables an RSpec::Mocks::TestDouble or any other object to act as a test spy. Delegates to RSpec::Mocks to configure the allowing of the messages

In particular, spy_on makes this act of configuring real objects as spies easier by providing more concise syntax for configuring spying on multiple methods while forwarding the messages along to the original implementations.

Examples:

person = Person.new("Ally", 22)
spy_on(person, :name, :age)
# use `person` in a test
expect(person).to have_received(:name)
expect(person).to have_received(:age)

Parameters:

  • target (Object/RSpec::Mocks::TestDouble)

    The object to be spied on

  • messages (Array)

    Array of symbols for messages that should configured for spying

Returns:

  • (target)


22
23
24
25
26
27
28
29
# File 'lib/rspec/spy_on/example_methods.rb', line 22

def spy_on(target, *messages)
  if target.is_a?(RSpec::Mocks::TestDouble)
    messages.each { |m| RSpec::Mocks.allow_message(target, m) }
  else
    messages.each { |m| allow_partial(target, m) }
  end
  target
end