10
votes

I try to debug my user_mailer.rb within my test environment. But I dont know why the debugger doesnst stop where it suppose to.

So, the code I roughly have is: user_mailer_spec.rb

describe UserMailer do
  describe '#send_notification_letters' do
    # bunch of code omitted here ...
    it 'should record itself to the database'
      expect { UserMailer.send_notification_letters(user) }.to change{SentMail.count}.by(1)
    end
  end
end

In user_mailer.rb

class UserMailer < ActionMailer::Base
  def send_notification_letters(user)
    byebug # should break here but doesnt, 
           # also tried binding.pry here, also doesnt work
    # buggy code ...
    # buggy code ...
    # buggy code ...

    SentMail.create(...) # never reached
    mail(to:..., from:...)
  end
end

The question is, why is byebug/pry not stopping in the user_mail.rb when i run the test rspec spec/mailer/user_mailer_spec.rb?

And why?

How to make it stop at that break point?

Is there a bug in the debugger?

3
What do you get in the logs?Mohammad AbuShady
This happens to me frequently unless I also add require 'byebug' above the byebug line.infused
@MohammadAbuShady, there are only bunch of SQL transaction logs in test.log, which means UserMailer. send_notification_letters() is executed. But the debugger is not stopped. Nothing in the logs about the debugger.Sida Zhou
@infused, tried require 'byebug', did nothing for meSida Zhou
does adding the byebug before the expect work ?Mohammad AbuShady

3 Answers

2
votes

UserMailer.send_notification_letters(user) does not actually call the the send_notification action, but instead it returns an ActionMailer::MessageDelivery object. You need to invoke the delivery in order to hit the method, like this:

UserMailer.send_notification_letters(user).deliver_now

You can read more on the topic in http://api.rubyonrails.org/classes/ActionMailer/Base.html#class-ActionMailer::Base-label-Sending+mail

1
votes

I've run into the same situation today. After digging around, I've found my issue is caused by the thin server's daemonize configuration.

Edit your config/thin.yml:

daemonize: false

Or you can just comment out the thin gem in your Gemfile, use the default WEBrick instead.

0
votes

I had the same problem, just restart the server.

In my case puma, and without spring.