# Testing errors on a mailer

**URL:** <https://www.ruby-forum.com/t/testing-errors-on-a-mailer/92733>\
**Category:** Rails\
**Created:** [April 23, 2007, 4:56pm UTC](https://www.ruby-forum.com/t/testing-errors-on-a-mailer/92733 "2007-04-23T16:56:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bjhess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce7236/32.png) [@bjhess](https://www.ruby-forum.com/u/bjhess)\
**Post date:** [April 23, 2007, 4:56pm UTC](https://www.ruby-forum.com/t/testing-errors-on-a-mailer/92733/1 "2007-04-23T16:56:28Z")

</div>

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://snippets.dzone.com/posts/show/1872)  
[http://project.ioni.st/post/692#post-692](http://project.ioni.st/post/692#post-692)

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![bjhess](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/ce7236/32.png) [@bjhess](https://www.ruby-forum.com/u/bjhess)\
**Post date:** [April 25, 2007, 3:20am UTC](https://www.ruby-forum.com/t/testing-errors-on-a-mailer/92733/2 "2007-04-25T03:20:24Z")

</div>

You could use Mocha ([http://mocha.rubyforge.org](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.

> **[James Mead](https://jamesmead.org/)**
>
> Ruby Developer, author of Mocha, and ex-Antarctic Hero.
