Forum: Ruby on Rails Testing errors on a mailer

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
bjhess (Guest)
on 2007-04-23 18:56
(Received via mailing list)
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!
James M. (Guest)
on 2007-04-25 05:20
(Received via mailing list)
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
This topic is locked and can not be replied to.