Commit 2f9959c

mokha <mokha@cisco.com>
2019-04-15 22:57:19
move create emails to before :each
1 parent 434d647
Changed files (2)
lib
minbox
spec
lib/minbox/inbox.rb
@@ -5,10 +5,7 @@ module Minbox
     include Singleton
     include Enumerable
 
-    attr_accessor :seconds
-
-    def initialize(root_dir: 'tmp', seconds: 1)
-      @seconds = seconds
+    def initialize(root_dir: 'tmp')
       empty!
       ::Listen.to(File.expand_path(root_dir), only: /\.eml$/) do |modified, added, removed|
         added.each do |file|
@@ -18,12 +15,12 @@ module Minbox
     end
 
     def emails(count: 0)
-      wait_until { |x| x.count >= count } if count > 0
+      wait_until { |x| @emails.keys.count >= count } if count > 0
 
       @emails.keys
     end
 
-    def wait_until(seconds: self.seconds, wait: 0.1)
+    def wait_until(seconds: 10, wait: 0.1)
       iterations = (seconds / wait).to_i
       iterations.times do
         return true if yield(self)
spec/minbox/inbox_spec.rb
@@ -3,28 +3,28 @@
 RSpec.describe Minbox::Inbox do
   subject! { described_class.instance }
 
-  def create_emails
-    fork do
-      IO.write("tmp/1.eml", Mail.new do
-        to Faker::Internet.email
-        from Faker::Internet.email
-        subject "hello world"
-      end.to_s)
-      IO.write("tmp/2.eml", Mail.new do
-        to Faker::Internet.email
-        from Faker::Internet.email
-        subject "goodbye world"
-      end.to_s)
-    end
+  before do
+    subject.empty!
+
+    IO.write("tmp/1.eml", Mail.new do
+      to Faker::Internet.email
+      from Faker::Internet.email
+      subject "hello world"
+    end.to_s)
+    IO.write("tmp/2.eml", Mail.new do
+      to Faker::Internet.email
+      from Faker::Internet.email
+      subject "goodbye world"
+    end.to_s)
   end
 
-  before do
+  after do
     FileUtils.rm(Dir.glob('tmp/*.eml'))
   end
 
   describe "#empty!" do
     before :example do
-      IO.write("tmp/1.eml", Mail.new do
+      IO.write("tmp/3.eml", Mail.new do
         to Faker::Internet.email
         from Faker::Internet.email
         subject "hello world"
@@ -36,15 +36,11 @@ RSpec.describe Minbox::Inbox do
   end
 
   describe "#emails" do
-    before { create_emails }
-
     specify { expect(subject.emails(count: 2)).to match_array(['1.eml', '2.eml']) }
   end
 
   describe "#wait_until!" do
     context "when the condition is satisfied" do
-      before { create_emails }
-
       specify { expect(subject.emails(count: 2)).to match_array(['1.eml', '2.eml']) }
     end
 
@@ -58,8 +54,6 @@ RSpec.describe Minbox::Inbox do
   end
 
   describe "#open" do
-    before { create_emails }
-
     context "when opening an email in the inbox" do
       let(:result) { subject.open('2.eml') }