Sebastian W.
2008-Dec-04 07:58 UTC
[rspec-users] The RSpec way of doing this? Need help on validating block
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/.
Sebastian W.
2008-Dec-04 07:59 UTC
[rspec-users] The RSpec way of doing this? Need help on validating block
Yikes, left out the all-important call:> 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?es.send_email> end > endSorry about that. -- Posted via http://www.ruby-forum.com/.
Sebastian W.
2008-Dec-04 08:07 UTC
[rspec-users] The RSpec way of doing this? Need help on validating block
Ha! Don''t I feel silly. Just figured it out, I think. Sebastian W. wrote:> Yikes, left out the all-important call: > >> 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 >> endmock_smtp = mock("smtp") MockSMTP.should_receive(:start).with(''your.smtp.server'', 25).and_yield(mock_smtp) mock_smtp.should_recieve(:send_message).with("Yay! You got an email!", ''your at mail'', ''other at mail'')> es.send_email >> end >> endMission accomplished. :P -- Posted via http://www.ruby-forum.com/.
Pat Maddox
2008-Dec-04 17:52 UTC
[rspec-users] The RSpec way of doing this? Need help on validating block
Glad I could help! "Sebastian W." <lists at ruby-forum.com> writes:> Ha! Don''t I feel silly. Just figured it out, I think. > > Sebastian W. wrote: >> Yikes, left out the all-important call: >> >>> 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 > mock_smtp = mock("smtp") > MockSMTP.should_receive(:start).with(''your.smtp.server'', > 25).and_yield(mock_smtp) > mock_smtp.should_recieve(:send_message).with("Yay! You got an > email!", ''your at mail'', > ''other at mail'') >> es.send_email >>> end >>> end > > Mission accomplished. :P