Testing errors on a mailer


#1

I have the following action written:

POST /designers.xml;contact

def contact
begin
DesignerMailer.deliver_contact(params)
rescue Exception => e
logger.error(“Email from #{params[:fromEmail]} to
#{params[:toEmail]} errored with: #{e}”)
respond_to do |format|
format.xml { render :xml => e.to_xml }
end
else
respond_to do |format|
format.xml { head :ok }
end
end
end

As you can see, I follow a different path if there are errors in email
delivery. My question?

How do I force that exception in a functional test? A simple mock is
not sufficient since I only want the exception to occur in one of my
tests. I’ve looked at the following code, but I’m not sure it fits
what I need to do:

http://snippets.dzone.com/posts/show/1872
http://project.ioni.st/post/692#post-692

Thanks in advance!


#2

You could use Mocha (http://mocha.rubyforge.org) to do something like
the following…

def test_what_happens_when_exception_raised
DesignerMailer.stubs(:deliver_contact).raises(Exception)

post :contact

assertions

end

def test_what_happens_when_no_exception_raised
DesignerMailer.stubs(:deliver_contact)
get :contact

post :contact

assertions

end


James.
http://blog.floehopper.org