Michael Michael - 2 years ago 73
Ruby Question

How to test error handling with RSpec?

I try to test this code:

mail = Mail.new
mail.from = @from
mail.to = to
return false
return true

for error handling with RSpec:

it 'should return true if success' do
expect(mailer.send_confirmation_request(token)).to be true

it 'should return false if fails' do
allow_any_instance_of(Mail).to receive(:deliver).and_raise('Mail error')
expect(mailer.send_confirmation_request(token)).to be false

But it doesn't work: second test failed. Why does
) not work in this way? How that can be tested? Should am I fix my code to be more testable? I can test it by stubbing Mail#new with rspec double, but it leads to stubbing all mail methods and not looks like a right way to do.

If it does matter, I use mikel's mail gem and make tests according to his recommendations.

Answer Source

Mail.new doesn't return an instance of Mail, it returns an instance of Mail::Message, so you're calling allow_any_instance_of on the wrong class.

I assume the library does this to simplify the interface, but it is a little counterintuitive.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download