Skip to content

Instantly share code, notes, and snippets.

@snikch
Created February 5, 2014 22:18
Show Gist options
  • Select an option

  • Save snikch/8834442 to your computer and use it in GitHub Desktop.

Select an option

Save snikch/8834442 to your computer and use it in GitHub Desktop.
Apostle Rails testing

Testing an apostle-rails Integration

This document gives examples using RSpec, but the concepts should apply to any testing framework.

Turn off delivery

In test, we recommend turning off delivery. You can do this in a spec helper, or similar. All calls to #deliver will return true.

require 'apostle'

# Don't attempt to deliver in tests
Apostle.configure do |config|
  config.deliver = false
end

Testing Mailers

Unit testing a mailer is quite straight forward. Any action mailer method call using apostle-rails will return an instance of Apostle::Mail, which you can assert the attributes of.

For example, MyMailer.send_welcome(user) would return an instance of Apostle::Mail.

See membership_order_mailer_spec.rb for more information on testing an Apostle::Mail instance.

Testing Integration

If you are testing from further out in your code, we recommend simply testing that a mailer has received the object(s) that it expects, and leave the rest to your mailer unit test.

mock_mailer = {}
expect(MyMailer).to receive(:send_welcome).with(user).and_return(mock_mailer)
expect(mock_mailer).to receive(:deliver!)
# encoding: utf-8
require "spec_helper"
describe MembershipOrderMailer do
describe "welcome" do
let(:membership) { create(:membership) }
# 'mail' is an instance of Apostle::Mail
# https://github.com/apostle/apostle-ruby/blob/master/lib/apostle/mail.rb
let(:mail) { MembershipOrderMailer.welcome(
membership.user.id,
membership.membership_plan.id
) }
it "sends to the user" do
# For a list of attributes you can assert, check
# https://github.com/apostle/apostle-ruby/blob/master/lib/apostle/mail.rb
# Anything not explicitly set as an attribute will be assigned to the 'data' hash
mail.email.should eq(membership.user.email)
mail.data.should eq({ order: { price: 19.00 } })
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment