2

I'm trying out email_spec, which says it supports Pony, but I'm not sure how I'd go about testing emails in a sinatra app. The examples in the readme show usages with rails ActionMailer, but not in Pony.

Not precious about using email_spec, so any other ideas for testing emails using rspec in sinatra are welcomed =)

zlog
  • 3,316
  • 4
  • 42
  • 82

2 Answers2

3

I ended up looking at the pony spec file, and stealing code from it to write my specs =)

This is what I have:

./spec/spec_helper.rb

def do_not_send_email
  Pony.stub!(:deliver)  # Hijack deliver method to not send email
end

RSpec.configure do |conf|
  conf.include Rack::Test::Methods
  conf.mock_with :rspec

  # ...

  conf.before(:each) do
    do_not_send_email
  end
end

./spec/integration/invite_user_spec.rb

require_relative '../spec_helper'

feature "Invite user" do
  scenario "should send an invitation email" do
    visit "/"
    click_link "start-btn"

    within_fieldset("Invite new user") do
      fill_in 'new_user_email', :with => 'franz@gmail.com'

      Pony.should_receive(:mail) { |params|
        params[:to].should == "franz@gmail.com"
        params[:subject].should include("You are invited to blah")

        params[:body].should include("You've been invited to blah")
        params[:body].should include("/#{@account_id}/new-user/register")
      }
      click_button 'new_user'
    end

    page.should have_content("Invitation email has been sent")
  end
end
zlog
  • 3,316
  • 4
  • 42
  • 82
2

I haven't used email_spec before, but it looks pretty cool.

You can see how I test pony in the spec file, which may work for your case:

https://github.com/benprew/pony/blob/master/spec/pony_spec.rb

Also, you can deliver messages via :test, and it will use the test mailer included with mail:

See "Using Mail with Testing or Spec'ing Libraries" at https://github.com/mikel/mail for an example of examining the test messages.

ben.prew
  • 56
  • 6
  • That's pretty much what I did - stole code from you your spec file ;) – zlog Dec 18 '11 at 12:35
  • This answer (from the gem's maintainer) should be the accepted answer, as there is no need for mocks & stubbing. For the sake of completeness this is how you stop live mail delivery in tests; `Pony.override_options = { via: :test }`. The 'sent' mail can then be accessed at `Mail::TestMailer.deliveries.first`. The rest, including RSpec matchers is covered in the Mail gem's README here https://github.com/mikel/mail#using-mail-with-testing-or-specing-libraries – MatzFan Aug 02 '18 at 14:04