Skip to content

Latest commit

 

History

History
31 lines (22 loc) · 703 Bytes

mock-method-chain-calls-with-rspec.md

File metadata and controls

31 lines (22 loc) · 703 Bytes

Mock Method Chain Calls With RSpec

Generally with RSpec you mock one method call at a time:

allow(User).to receive(:new).and_return(true)

Sometimes you are dealing with code that involves a chain of method calls.

User
  .new
  .approve
  .send_welcome_email

If it becomes unreasonable to mock out each individual method, you can instead mock out the chain of calls.

allow(User).to receive_message_chain('new.approve.send_welcome_email')

Alternatively, you can write this as:

allow(User).to receive_message_chain(:new, :approve, :send_welcome_email)

source