Carson Reinke's Headshot

Carson Reinke's (πŸš—β˜€β˜”πŸ”‘) Blog

Mocking SMTP in Unit Tests

I’m sure there are other better ways to do this, but I just wanted to try doing something real quick using MailCatcher.

Instead of just starting a process up, I added the following to the test_helper.rb

mailcatcher_process_id = Kernel.fork do 
    require 'mail_catcher'!(:daemon => false) 
$stderr.puts('Unable to fork mailcatcher') if mailcatcher_process_id.nil?() 
Kernel.at_exit do 
    unless mailcatcher_process_id.nil?()
        Process.kill('TERM', mailcatcher_process_id) rescue nil 
        Process.kill('SIGTERM', mailcatcher_process_id) rescue nil 
ruby - rails - smtp