Skip to content
Browse files

Add error handling to the mailer

Change the mailer so when the actual email fails (i.e. the username
and password are wrong) an error message saying so is displayed to the
user.

Issue #3
  • Loading branch information...
1 parent 849830e commit fad5f239b58aad0d515d1b5259e1bc0334f045bd @rhburrows committed
Showing with 32 additions and 6 deletions.
  1. +7 −0 lib/reviewr/mailer.rb
  2. +25 −6 spec/reviewr/mailer_spec.rb
View
7 lib/reviewr/mailer.rb
@@ -2,6 +2,8 @@
module Reviewr
class Mailer
+ attr_reader :project
+
def initialize(project)
@project = project
end
@@ -21,6 +23,11 @@ def send(body)
:auth => :plain,
:domain => @project.email_server
})
+ true
+ rescue => e
+ $stderr.puts "Error sending email:"
+ $stderr.puts e.inspect
+ false
end
end
end
View
31 spec/reviewr/mailer_spec.rb
@@ -3,12 +3,17 @@
module Reviewr
describe Mailer do
describe "#send" do
+ let(:mailer){ Mailer.new(double("Project").as_null_object) }
+
+ before do
+ Pony.stub(:mail)
+ end
+
it "calls Pony#mail with the information from the project" do
- project = double("Project",
- :user_email => 'from',
- :to => 'to',
- :email_server => 'host',
- :email_password => 'p')
+ mailer.project.stub(:user_email).and_return('from')
+ mailer.project.stub(:to).and_return('to')
+ mailer.project.stub(:email_server).and_return('host')
+ mailer.project.stub(:email_password).and_return('p')
Pony.should_receive(:mail).with(
hash_including(:from => 'from',
:to => 'to',
@@ -24,7 +29,21 @@ module Reviewr
:auth => :plain,
:domain => 'host'
}))
- Mailer.new(project).send('body')
+ mailer.send('body')
+ end
+
+ context "successful sending" do
+ it "returns true" do
+ mailer.send('body').should be_true
+ end
+ end
+
+ context "unsuccessful sending" do
+ it "returns false" do
+ $stderr.stub(:puts)
+ Pony.stub(:mail).and_raise("Error sending")
+ mailer.send('body').should be_false
+ end
end
end
end

0 comments on commit fad5f23

Please sign in to comment.
Something went wrong with that request. Please try again.