Skip to content

Instantly share code, notes, and snippets.

@marksim
Created July 10, 2012 16:18
Show Gist options
  • Save marksim/3084425 to your computer and use it in GitHub Desktop.
Save marksim/3084425 to your computer and use it in GitHub Desktop.
How to isolate mail dependency without integrating into data model?

I'm not sure how to remove the dependency on VerificationMailer without pushing it into the profile model and making the model suddenly care about how to mail verifications.

I WANT to remove it so I don't have any need to load up rails when running the tests, but also want to keep my models sensible and without a lot of knowledge of business logic.

Example usage:

profile = Profile.find(1)
profile.extend Verifier
profile.verify(email: 'myemail@example.com')
require 'securerandom'
module Verifier
def verify(hash)
v = verifications.build(hash)
v.token = SecureRandom.hex(16)
if v.valid?
v.save
VerificationMailer.basic(v.id).deliver
end
v
end
end
require 'fast_spec_helper'
require 'verifier'
describe Verifier do
let(:subject) {
s = OpenStruct.new
s.verifications = mock
s.extend Verifier
}
let(:verification) { OpenStruct.new }
it "queues up an e-mail if the verification is valid" do
subject.verifications.stub(:build => verification)
verification.stub(:valid? => true)
VerificationMailer.should_receive(:basic).and_return(stub.as_null_object)
subject.verify({})
end
end
@coreyhaines
Copy link

Create a coordinator object that creates the profile, deals with the model, etc, then send the mail. Test that.

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