Skip to content

Instantly share code, notes, and snippets.

@pierrax
Last active May 24, 2016 14:55
Show Gist options
  • Save pierrax/b39d28abd6afb268e8cf483ff330a0d8 to your computer and use it in GitHub Desktop.
Save pierrax/b39d28abd6afb268e8cf483ff330a0d8 to your computer and use it in GitHub Desktop.
Spy pattern

Use spy

Spies are an alternate type of test double that support this pattern by allowing you to expect that a message has been received after the fact, using have_received.

RSpec.describe "have_received" do
  it "passes when the message has been received" do
    invitation = spy('invitation')
    invitation.deliver
    expect(invitation).to have_received(:deliver)
  end
end

Alternative way

RSpec.describe "have_received" do
  it "passes when the message has been received" do
    # omitted code
    expect_any_instance_of(Invitation).to receive(:have_received)
    
    post create, params
    
    expect(response.status).to eq(302)
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment