【问题标题】:Why doesnt byebug/pry stop at the breakpoint in Rspec ActionMailer?为什么 byebug/pry 不在 Rspec ActionMailer 的断点处停止?
【发布时间】:2015-05-21 13:54:03
【问题描述】:

我尝试在我的测试环境中调试我的 user_mailer.rb。但我不知道为什么调试器不会停在它应该停的地方。

所以,我大致拥有的代码是: 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

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

问题是,为什么当我运行测试 rspec spec/mailer/user_mailer_spec.rb 时 byebug/pry 没有停止在 user_mail.rb 中?

为什么?

如何让它在那个断点处停止?

调试器有错误吗?

【问题讨论】:

  • 你在日志中得到了什么?
  • 这种情况经常发生在我身上,除非我还在byebug 行上方添加require 'byebug'
  • @MohammadAbuShady,test.log 中只有一堆 SQL 事务日志,即 UserMailer。 send_notification_letters() 被执行。但是调试器并没有停止。日志中没有关于调试器的任何内容。
  • @infused,试过require 'byebug',对我没有任何帮助
  • 在期望工作之前添加 byebug 吗?

标签: ruby-on-rails rspec actionmailer pry byebug


【解决方案1】:

我今天也遇到了同样的情况。经过一番挖掘,我发现我的问题是由thin 服务器的daemonize 配置引起的。

编辑您的config/thin.yml

daemonize: false

或者你可以在你的 Gemfile 中注释掉 thin gem,使用默认的 WEBrick 代替。

【讨论】:

  • 我遇到了同样的问题,但我相信它不是thin 问题,因为我没有使用它。 :)
  • 这里也一样。我正在使用彪马。
  • 如果可以的话,我会投票一千次;一直让我发疯,谢谢!
【解决方案2】:

UserMailer.send_notification_letters(user) 实际上并没有调用send_notification 操作,而是返回一个ActionMailer::MessageDelivery 对象。您需要调用交付才能点击该方法,如下所示:

UserMailer.send_notification_letters(user).deliver_now

您可以在http://api.rubyonrails.org/classes/ActionMailer/Base.html#class-ActionMailer::Base-label-Sending+mail 中阅读有关该主题的更多信息

【讨论】:

    【解决方案3】:

    我遇到了同样的问题,只需重新启动服务器。

    在我的例子中,puma 没有弹簧。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-21
      • 2014-04-12
      • 2017-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-07
      • 1970-01-01
      相关资源
      最近更新 更多