Module: RSpecProxies::ProxyChains

Defined in:
lib/rspecproxies/proxy_chains.rb

Instance Method Summary collapse

Instance Method Details

#proxy_message_chain(message_chain, &last_proxy_setup_block) ⇒ Object

Sets up proxies all down the selector chain, until the last where a simple method stub is created and on which the block is called. Allows to set up stub chains quickly and safely.

message_chain : a dot separated message chain, like the standard message_chain expectation


13
14
15
# File 'lib/rspecproxies/proxy_chains.rb', line 13

def proxy_message_chain(message_chain, &last_proxy_setup_block)
  proxy_message_chain_a((message_chain.split('.').map &:intern), &last_proxy_setup_block)
end

#proxy_message_chain_a(messages, &last_proxy_setup_block) ⇒ Object

Same as #proxy_message_chain but using an array of symbols as message_chain instead of a dot separated string


19
20
21
22
23
24
25
26
27
28
# File 'lib/rspecproxies/proxy_chains.rb', line 19

def proxy_message_chain_a(messages, &last_proxy_setup_block)
  first_message = messages.first
  return last_proxy_setup_block.call(receive(first_message)) if messages.size == 1

  receive(first_message).and_wrap_original do |m, *args, &original_block|
    result = m.call(*args, &original_block)
    allow(result).to proxy_message_chain_a(messages.drop(1), &last_proxy_setup_block)
    result
  end
end