gpt4 book ai didi

ruby-on-rails - Rails 测试计划的邮件抽取任务

转载 作者:太空宇宙 更新时间:2023-11-03 16:47:02 25 4
gpt4 key购买 nike

我有一个 rake 任务,它应该每天向用户发送一封邮件,该用户的 daily 字段 == true。 (我的项目中已经有一个正在运行的邮件实现,它会在创建帐户和重置密码时发送邮件)。我正在使用 Whenever gem

问题是,因为我不在生产中,所以我不知道如何测试它是否有效。

这是我的 UserMailer :

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

我的抽佣任务:

desc“向用户发送电子邮件”

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

还有我的 schedule.rb :

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

我在看了教程之后做了这个:

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

我尝试执行 crontab -l,我得到了这个:

# 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

它们是测试(或了解)我的任务是否适用于生产的一种方式吗?

更新:

我尝试运行“rake email_sender”,我在 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--

因此它向仅有的两个拥有 daily == true 的用户发送邮件,所以我的方法有效。但是预定的工作是否有效?

最佳答案

将 UserMailer 更改为

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

关于ruby-on-rails - Rails 测试计划的邮件抽取任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31250310/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com