[rspec-users] Specing raising error, handling, and then not raising error

Mikel Lindsaar raasdnil at gmail.com
Sun Nov 4 04:55:54 EST 2007

Hey guys and gals,

I have a snippet of code:

Net::SMTP(@host, @port, @from_domain) do |smtp|
  @emails.each do |email|
      smtp.send_message email.encoded, email.from, email.destinations
      @emails_sent += 1
    rescue Exception => e
      # blah

What I want to do is:

Say there are 4 emails.

First email is sent OK
On the second email smtp raises a IOError error
The third and fourth emails are sent OK

I want to spec:

That the second iteration raises an error.
That the total emails sent count changed by 3

I can't seem to figure out how to stub/mock the smtp object to return
an email on the 1, 3, and 4th iteration and raise an error on the 2nd

I thought something like this, but it doesn't work:

  it "should only increase the sent emails counter if the email was sent" do
    @smtp = mock(Net::SMTP)
IOError, true, true)
    @sender = Mailer::Sender.new(valid_args(:emails => @emails))
    @sender.sent_emails.should == 3

If anyone has any pointers, that would be great.

I am using rSpec trunk - today's release.


More information about the rspec-users mailing list