我为一名工作人员编写了一个测试,该工作人员会收到电子邮件,然后对他们做一些事情:
Net::POP3.start('pop.gmail.com', 995, "xxx", "xxxx") do |pop|
if pop.mails.empty?
log "#{Time.now} No mail."
else
pop.each_mail do |mail|
#do something
end
end
end
以这种方式存根的最佳方法是什么Net::POP3.start
,它会在实际运行时返回类似的数据?
谢谢
迪基
编辑:
其余的工作/填写#do something
看起来有点像这样:
Net::POP3.start('pop.gmail.com', 995, "xxx", "xxxx") do |pop|
if pop.mails.empty?
log "#{Time.now} No mail."
else
pop.each_mail do |mail|
parse_mail mail.pop
end
end
end
def parse_mail(raw_email)
email = Mail.new raw_email
email.attachments
email.from
email.subject
end
我想出的解决方案是(这可能有点特定于您的需求):
Net::POP3.stub(:start).and_yield Net::POP3.new("a test string")
Net::POP3.any_instance.stub(:mails).and_return [Net::POPMail.new("test","test","test","test")]
Net::POPMail.any_instance.stub(:pop).and_return("a raw email string")
我真的不喜欢。
使用@SteveTurczyn 正在使用的一些技术对其进行重构。