Commit a18317f

mokha <mokha@cisco.com>
2019-04-15 21:54:20
collapse start into initialize
1 parent 9528ec4
Changed files (2)
lib
minbox
spec
lib/minbox/inbox.rb
@@ -5,11 +5,8 @@ module Minbox
   class Inbox
     include Enumerable
 
-    def initialize
+    def initialize(root_dir: 'tmp')
       empty!
-    end
-
-    def start(root_dir: 'tmp')
       ::Listen.to(File.expand_path(root_dir), only: /\.eml$/) do |modified, added, removed|
         added.each do |file|
           @emails[File.basename(file)] = Mail.read(file)
spec/minbox/inbox_spec.rb
@@ -1,11 +1,26 @@
 # frozen_string_literal: true
 
 RSpec.describe Minbox::Inbox do
-  subject { described_class.new }
+  subject! { described_class.new }
+
+  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)
+      sleep 1.5
+    end
+  end
 
   before do
     FileUtils.rm(Dir.glob('tmp/*.eml'))
-    subject.start
   end
 
   describe "#empty!" do
@@ -22,55 +37,26 @@ RSpec.describe Minbox::Inbox do
   end
 
   describe "#emails" do
-    before :example do
-      pid = 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)
-        sleep 0.3
-      end
-      Process.wait(pid)
-      @result = subject.emails
-    end
+    let(:results) { subject.emails }
+
+    before { Process.wait(create_emails) }
 
-    specify { expect(@result).to match_array(['1.eml', '2.eml']) }
+    specify { expect(results).to match_array(['1.eml', '2.eml']) }
   end
 
   describe "#open" do
-    before :example do
-      pid = 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)
-        sleep 0.3
-      end
-      Process.wait(pid)
-    end
+    before { Process.wait(create_emails) }
 
     context "when opening an email in the inbox" do
-      before { @result = subject.open('2.eml') }
+      let(:result) { subject.open('2.eml') }
 
-      specify { expect(@result.subject).to eql('goodbye world') }
+      specify { expect(result.subject).to eql('goodbye world') }
     end
 
     context "when opening an email not in the inbox" do
-      before { @result = subject.open("#{SecureRandom.uuid}.eml") }
+      let(:result) { subject.open("#{SecureRandom.uuid}.eml") }
 
-      specify { expect(@result).to be_nil }
+      specify { expect(result).to be_nil }
     end
   end
 end