[rspec-users] The RSpec way of doing this? Need help on validating block

Sebastian W. lists at ruby-forum.com
Thu Dec 4 02:58:54 EST 2008


Hello, I'm back again with more questions about mocks and how to do good
testing in general. Let's say I'm writing this EmailSender class and I
want to make it totally awesomely tested with RSpec. Here's how far I've
gotten so far:

require 'net/smtp'

class EmailSender
  def send_email
    mailer.start('your.smtp.server', 25) do |smtp|
      smtp.send_message("Yay! You got an email!", 'your at mail',
'other at mail')
    end
  def

  def mailer
    Net::SMTP
  end
end

describe EmailSender do
  it "Should use Net::SMTP to send email"
    es = EmailSender.new
    es.mailer.should == Net::SMTP
    MockSMTP = mock("Net::SMTP")
    def es.mailer
      MockSMTP
    end
    MockSMTP.should_receive(:start).with('your.smtp.server', 25)
    #but, er...wha?
  end
end

So - not sure how to validate the part where "send_message" is called.
Is there a recommended way of doing this?
-- 
Posted via http://www.ruby-forum.com/.


More information about the rspec-users mailing list