Skip to content

Instantly share code, notes, and snippets.

@markjlorenz
Last active August 29, 2015 14:02
Show Gist options
  • Save markjlorenz/cd63d4ea744ba9ae4023 to your computer and use it in GitHub Desktop.
Save markjlorenz/cd63d4ea744ba9ae4023 to your computer and use it in GitHub Desktop.
An amazing method extraction

Pre-Extaction:

describe "email side effects" do
  it "sent an email to the PIC" do
    emails_to_pic = to_addresses.select { |to| to == pic.email }
    expect(emails_to_pic.size).to eq(1)
  end

  context "PIC is on vacation" do
    it "sent an email to the backup" do
      emails_to_pic = to_addresses.select { |to| to == pic.email }
      emails_to_backup = to_addresses.select { |to| to == backup_pic.email }
      fail "no PIC email expected" unless emails_to_pic.empty?

      expect(emails_to_backup.size).to eq(1)
    end

    let(:pic) { din_engineer(on_vacation_until: Date.today + 1.day) }
    let(:backup_pic) { din_engineer }
    let(:platform) do
      din_platform do |p|
         p.primary_engineer_id = pic.id
         p.secondary_engineer_id = backup_pic.id
      end
    end

    context "there is no backup engineer" do
      it "emails the PIC anyway" do
        emails_to_pic = to_addresses.select { |to| to == pic.email }
        expect(emails_to_pic.size).to eq(1)
      end

      let(:backup_pic) { double(id: nil) }
    end

  end

  let(:to_addresses) do
    ActionMailer::Base.deliveries.map(&:to).flatten
  end
end

Post-Extraction

describe "email side effects" do
  it "sent an email to the PIC" do
    expect(emails_to(pic).size).to eq(1)
  end

  context "PIC is on vacation" do
    it "sent an email to the backup" do
      fail "no PIC email expected" unless emails_to(pic).empty?
      expect(emails_to(backup_pic).size).to eq(1)
    end

    let(:pic) { din_engineer(on_vacation_until: Date.today+1.day) }
    let(:backup_pic) { din_engineer }
    let(:platform) do
      din_platform do |p|
         p.primary_engineer_id = pic.id
         p.secondary_engineer_id = backup_pic.id
      end
    end

    context "there is no backup engineer" do
      it "emails the PIC anyway" do
        expect(emails_to(pic).size).to eq(1)
      end
      let(:backup_pic) { double(id: nil) }
    end

  end

  let(:to_addresses) do
    ActionMailer::Base.deliveries.map(&:to).flatten
  end

  def emails_to(person)
    to_addresses.select { |to| to == person.email }
  end
end

I love how lines were either removed or only changed from emails_to_pic to emails_to(pic)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment