我正在为我的应用程序创建测试。
当创建一个询价请求时,我会向用户发送确认电子邮件。我还会向校对人员发送一封电子邮件进行通知。
我仍在学习Ruby,我相信有更好的方法来编写以下代码:
def email_exists?(email_address)
ActionMailer::Base.deliveries.each do |m|
if m[:to].to_s == email_address
return true
end
end
end
def email_with_subject_exists?(email_address, email_subject)
ActionMailer::Base.deliveries.each do |m|
if m[:to].to_s == email_address && m[:subject].to_s == email_subject
return true
else
return false
end
end
end
我发现如果我不包含“return false”,那么邮件的整个内容都会被输出。如有改进此代码的帮助将不胜感激。
以下是ActionMailer :: Base.deliveries的内容:
[#<Mail::Message:70094817191440, Multipart: true, Headers: <Date: Thu, 01 Sep 2016 15:50:35 +0700>, <From: from@example.com>, <To: peterjohnson1@example.com>, <Message-ID: <57c7ebdb1b5f4_2ab93fc03545e1f850559@Mitchell-Goulds-MacBook-Air.local.mail>>, <Subject: Welcome to the ProvenWord team!>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_57c7ebdb16e49_2ab93fc03545e1f85046d"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>>, #<Mail::Message:70094829223120, Multipart: true, Headers: <Date: Thu, 01 Sep 2016 15:50:35 +0700>, <From: from@example.com>, <To: peterjohnson2@example.com>, <Message-ID: <57c7ebdb23188_2ab93fc03545e1f850725@Mitchell-Goulds-MacBook-Air.local.mail>>, <Subject: Welcome to the ProvenWord team!>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_57c7ebdb229aa_2ab93fc03545e1f8506be"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>>, #<Mail::Message:70094772574660, Multipart: true, Headers: <Date: Thu, 01 Sep 2016 15:50:35 +0700>, <From: from@example.com>, <To: peterjohnson3@example.com>, <Message-ID: <57c7ebdb28936_2ab93fc03545e1f850960@Mitchell-Goulds-MacBook-Air.local.mail>>, <Subject: Welcome to the ProvenWord team!>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_57c7ebdb27bf6_2ab93fc03545e1f850835"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>>, #<Mail::Message:70094829778220, Multipart: true, Headers: <Date: Thu, 01 Sep 2016 15:50:35 +0700>, <From: from@example.com>, <To: peterjohnson4@example.com>, <Message-ID: <57c7ebdb2c982_2ab93fc03545e1f85116b@Mitchell-Goulds-MacBook-Air.local.mail>>, <Subject: Welcome to the ProvenWord team!>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_57c7ebdb2c064_2ab93fc03545e1f85104b"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>>, #<Mail::Message:70094829991680, Multipart: true, Headers: <Date: Thu, 01 Sep 2016 15:50:35 +0700>, <From: from@example.com>, <To: peterjohnson5@example.com>, <Message-ID: <57c7ebdb30c2b_2ab93fc03545e1f85135c@Mitchell-Goulds-MacBook-Air.local.mail>>, <Subject: Welcome to the ProvenWord team!>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_57c7ebdb30465_2ab93fc03545e1f85123c"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>>, #<Mail::Message:70094777280260, Multipart: true, Headers: <Date: Thu, 01 Sep 2016 15:50:35 +0700>, <From: from@example.com>, <To: johnpeters@example.com>, <Message-ID: <57c7ebdbb8421_2ab93fc03545e1f8515e5@Mitchell-Goulds-MacBook-Air.local.mail>>, <Subject: Your ProvenWord quotation request has been delivered>, <Mime-Version: 1.0>, <Content-Type: multipart/alternative; boundary="--==_mimepart_57c7ebdbb76fa_2ab93fc03545e1f8514ee"; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>>]
[1,2].each { |i| i == 1 }
返回[1,2]
,所以你的函数正在返回ActionMailer::Base.deliveries
。你的 #each 正在执行工作但没有做/返回任何东西。 - Peter R[1,2].select {|i| i>0 }
返回[1,2]
。#find返回第一个true[1,2].find {|i| i>0 }
返回1
。 - Peter R