How do I test Pony emailing in a Sinatra app, usin

2019-07-20 02:07发布

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 =)

2条回答
▲ chillily
2楼-- · 2019-07-20 02:33

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.

查看更多
Explosion°爆炸
3楼-- · 2019-07-20 02:52

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
查看更多
登录 后发表回答