首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >存根网络::POP3.start

存根网络::POP3.start
EN

Stack Overflow用户
提问于 2014-06-13 18:55:13
回答 1查看 168关注 0票数 0

我正在为一个工人写一个测试,它接收电子邮件,然后用它们做一些事情:

代码语言:javascript
复制
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看起来有点像这样:

代码语言:javascript
复制
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

我想出的解决方案是(这可能对您的需求有点具体):

代码语言:javascript
复制
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使用的一些技术对其进行重构。

EN

回答 1

Stack Overflow用户

发布于 2014-06-13 21:53:52

下面是几封电子邮件的“快乐之路”。

代码语言:javascript
复制
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

end
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24203675

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档