开发者

testing emails with capybara and delayed_job

inspired by the episode of railscast (http://railscasts开发者_C百科.com/episodes/275-how-i-test) i tried to add some request specs to my app.

using delayed_job for sending my emails, i did not found an easy way to test the sending of emails within my capybara test. i tried:

    it "emails user when requesting password reset" do
      ...(some user action that triggers sending an email)...
      Delayed::Worker.new.work_off
      ActionMailer::Base.deliveries.last.to.should include(user.email)
    end

thanks for any hints or help!


Much easier solution: Just create an initializer in config/initializers, e.g. delayed_job_config.rb

Delayed::Worker.delay_jobs = !Rails.env.test?

In your actual test you do not even need to know that delayed_job is deployed to send the mails, which keeps them clean and simple.


well, it should work. i found some misconfigurations in my app.

check you test env. you should set:

Staffomatic::Application.configure do
  ...
  config.action_mailer.delivery_method = :test
  config.action_mailer.default_url_options = { :host => "example.com" }
end

i added a line in the mailer macros module from the railscast to work off the Delayed::Job:

module MailerMacros
  def last_email
    Delayed::Worker.new.work_off
    ActionMailer::Base.deliveries.last
  end

  def reset_email
    Delayed::Job.destroy_all
    ActionMailer::Base.deliveries = []
  end
end

now you can check with:

last_email.to.should include(user.email)

your last email.

pretty easy!

ps. if you have the awesome mail_safe gem installed you should make sure it's not in the test env.!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜