简体   繁体   中英

Rails test a scheduled mail rake task

I have a rake task that is supposed to send a mail every day to users that has their daily field == true. (I already have a mail implementation in my project that is working, it sends a mail at the creation of an account and for password resetting). I'm using Whenever gem

The thing is, since I'm not in production, I don't know how I could test if it works.

Here is my UserMailer :

def daily_mail(user)
    @user = user
    mail to: user.email, subject: "Mail journalier"
end

My rake task :

desc "Send email to users"

task :email_sender => :environment do |_, args|
  User.find_each do |user|
    UserMailer.daily_mail(user).deliver if user.daily == true
  end
end

And my schedule.rb :

every :day, :at => '12pm' do # Use any day of the week or :weekend, :weekday
  rake "email_sender"
end

I did this after looking tutorials :

whenever --update-crontab store (and tried with --set too)

I tried to do a crontab -l and I get this :

# Begin Whenever generated tasks for: store

0 12 * * * /bin/bash -l -c 'cd /Users/Marco/Documents/TBProject && RAILS_ENV=production bundle exec rake email_sender --silent'

# End Whenever generated tasks for: store

# Begin Whenever generated tasks for: /Users/Marco/Documents/TBProject/config/schedule.rb

0 12 * * * /bin/bash -l -c 'cd /Users/Marco/Documents/TBProject && RAILS_ENV=production bundle exec rake email_sender --silent'

# End Whenever generated tasks for: /Users/Marco/Documents/TBProject/config/schedule.rb

Is their a way to test (or to know) if my task will work on production ?

UPDATE :

I tried to run "rake email_sender" and I got this in development.log :

[1m[36mUser Load (0.9ms)[0m  [1mSELECT  "users".* FROM "users"  ORDER BY "users"."id" ASC LIMIT 1000[0m
DEPRECATION WARNING: `#deliver` is deprecated and will be removed in Rails 5. Use `#deliver_now` to deliver immediately or `#deliver_later` to deliver through Active Job. (called from block (2 levels) in <top (required)> at /Users/Marco/Documents/TBProject/lib/tasks/rake1.rake:5)
  Rendered user_mailer/daily_mail.html.erb within layouts/mailer (1.2ms)
  Rendered user_mailer/daily_mail.text.erb within layouts/mailer (0.2ms)

UserMailer#daily_mail: processed outbound mail in 189.6ms

Sent mail to example@railstutorial.org (8.6ms)
Date: Mon, 06 Jul 2015 19:24:29 +0200
From: admin@fluxio.com
To: example@railstutorial.org
Message-ID: <559ab9cd79f6_193e3fc21dc6020412765@Marcos-MacBook-Air.local.mail>
Subject: Mail journalier
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_559ab9cd64da_193e3fc21dc6020412690";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_559ab9cd64da_193e3fc21dc6020412690
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

Daily Mail

----==_mimepart_559ab9cd64da_193e3fc21dc6020412690
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<html>
  <body>
    <h1>Daily Mail</h1>
  </body>
</html>

----==_mimepart_559ab9cd64da_193e3fc21dc6020412690--

DEPRECATION WARNING: `#deliver` is deprecated and will be removed in Rails 5. Use `#deliver_now` to deliver immediately or `#deliver_later` to deliver through Active Job. (called from block (2 levels) in <top (required)> at /Users/Marco/Documents/TBProject/lib/tasks/rake1.rake:5)
  Rendered user_mailer/daily_mail.html.erb within layouts/mailer (0.0ms)
  Rendered user_mailer/daily_mail.text.erb within layouts/mailer (0.0ms)

UserMailer#daily_mail: processed outbound mail in 15.2ms

Sent mail to test@test123.ch (2.7ms)
Date: Mon, 06 Jul 2015 19:24:29 +0200
From: admin@fluxio.com
To: test@test123.ch
Message-ID: <559ab9cdd27c_193e3fc21dc602041295@Marcos-MacBook-Air.local.mail>
Subject: Mail journalier
Mime-Version: 1.0
Content-Type: multipart/alternative;
 boundary="--==_mimepart_559ab9cdc753_193e3fc21dc60204128e8";
 charset=UTF-8
Content-Transfer-Encoding: 7bit


----==_mimepart_559ab9cdc753_193e3fc21dc60204128e8
Content-Type: text/plain;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

Daily Mail

----==_mimepart_559ab9cdc753_193e3fc21dc60204128e8
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit

<html>
  <body>
    <h1>Daily Mail</h1>
  </body>
</html>

----==_mimepart_559ab9cdc753_193e3fc21dc60204128e8--

So it sent mails to the only two users that had daily == true, so my method work. But is the scheduled job working ?

Change UserMailer to

def daily_mail(user)
    @user = user
    mail_to :user.email, subject: "Mail journalier"
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM