我正在为一个工人写一个测试,它接收电子邮件,然后用它们做一些事情:
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使用的一些技术对其进行重构。
发布于 2014-06-13 21:53:52
下面是几封电子邮件的“快乐之路”。
require 'spec_helper'
module NET
module POP3
end
end
describe "testing NET::POP3" do
before do
@mail1 = double('mail1')
@mail2 = double('mail2')
@mail3 = double('mail3')
@pop = double('pop')
@pop.stub_chain(:mails, :empty?).and_return false
allow(@pop).to receive(:each_mail).and_yield(@mail1).and_yield(@mail2).and_yield(@mail3)
expect(NET::POP3).to receive(:start).and_yield(@pop)
end
describe "three emails pending" do
it "will return a pop entity" do
NET::POP3.start do |p|
expect(p).to eq(@pop)
end
end
it "pop will indicate that emails are not empty" do
NET::POP3.start do |p|
expect(p.mails.empty?).to be_false
end
end
it "pop will contain three mails" do
NET::POP3.start do |p|
counter = 0
p.each_mail do |m|
counter += 1
expect([@mail1, @mail2, @mail3]).to include(m)
end
expect(counter).to eq 3
end
end
end
endhttps://stackoverflow.com/questions/24203675
复制相似问题