require 'spec_helper'
require 'pact/init/consumer'

describe 'The pact-init-consumer command line interface' do

  before do
    FileUtils.rm_rf('tmp')
    FileUtils.mkdir_p('tmp')
  end

  context 'no arguments' do

    it 'creates the desired files and folder structure' do
      Dir.chdir('tmp') do
        %x(bundle exec ../bin/pact-init-consumer)
        expect(Dir.exists?('spec/service_providers')).to eq(true)
        expect(File.exists?('spec/service_providers/pact_helper.rb')).to eq(true)
        expect(File.exists?('spec/service_providers/pact_helper.rb')).to eq(true)
      end
    end

  end

  context 'with consumer and provider argument' do

    it 'creates the desired files and folder structure' do
      Dir.chdir('tmp') do
        %x(bundle exec ../bin/pact-init-consumer --consumer \" Foo Consumer\" --provider \" Bar Provider \")
        expect(Dir.exists?('spec/service_providers')).to eq(true)
        expect(File.exists?('spec/service_providers/pact_helper.rb')).to eq(true)
        expect(File.exists?('spec/service_providers/pact_helper.rb')).to eq(true)
      end
    end

  end

end

require 'spec_helper'
require 'pact/init/consumer'

describe 'The pact-init-consumer command line interface' do

  before do
    FileUtils.mkdir_p('test')
    Dir.chdir('test')
  end

  after do
    Dir.chdir('..')
    FileUtils.rm_rf('test')
  end

  context 'no arguments' do

    it 'creates the desired files and folder structure' do
      %x(bundle exec ../bin/pact-init-consumer)
      expect(Dir.exists?('spec/service_providers')).to eq(true)
      expect(File.exists?('spec/service_providers/pact_helper.rb')).to eq(true)
      expect(File.exists?('spec/service_providers/pact_helper.rb')).to eq(true)
    end

  end

  context 'with consumer and provider argument' do

    it 'creates the desired files and folder structure' do
      %x(bundle exec ../bin/pact-init-consumer --consumer \" Foo Consumer\" --provider \" Bar Provider \")
      expect(Dir.exists?('spec/service_providers')).to eq(true)
      expect(File.exists?('spec/service_providers/pact_helper.rb')).to eq(true)
      expect(File.exists?('spec/service_providers/pact_helper.rb')).to eq(true)
    end

  end

end

--- 
+++ 
@@ -4,22 +4,19 @@
 describe 'The pact-init-consumer command line interface' do
 
   before do
-    FileUtils.mkdir_p('test')
-    Dir.chdir('test')
-  end
-
-  after do
-    Dir.chdir('..')
-    FileUtils.rm_rf('test')
+    FileUtils.rm_rf('tmp')
+    FileUtils.mkdir_p('tmp')
   end
 
   context 'no arguments' do
 
     it 'creates the desired files and folder structure' do
-      %x(bundle exec ../bin/pact-init-consumer)
-      expect(Dir.exists?('spec/service_providers')).to eq(true)
-      expect(File.exists?('spec/service_providers/pact_helper.rb')).to eq(true)
-      expect(File.exists?('spec/service_providers/pact_helper.rb')).to eq(true)
+      Dir.chdir('tmp') do
+        %x(bundle exec ../bin/pact-init-consumer)
+        expect(Dir.exists?('spec/service_providers')).to eq(true)
+        expect(File.exists?('spec/service_providers/pact_helper.rb')).to eq(true)
+        expect(File.exists?('spec/service_providers/pact_helper.rb')).to eq(true)
+      end
     end
 
   end
@@ -27,10 +24,12 @@
   context 'with consumer and provider argument' do
 
     it 'creates the desired files and folder structure' do
-      %x(bundle exec ../bin/pact-init-consumer --consumer \" Foo Consumer\" --provider \" Bar Provider \")
-      expect(Dir.exists?('spec/service_providers')).to eq(true)
-      expect(File.exists?('spec/service_providers/pact_helper.rb')).to eq(true)
-      expect(File.exists?('spec/service_providers/pact_helper.rb')).to eq(true)
+      Dir.chdir('tmp') do
+        %x(bundle exec ../bin/pact-init-consumer --consumer \" Foo Consumer\" --provider \" Bar Provider \")
+        expect(Dir.exists?('spec/service_providers')).to eq(true)
+        expect(File.exists?('spec/service_providers/pact_helper.rb')).to eq(true)
+        expect(File.exists?('spec/service_providers/pact_helper.rb')).to eq(true)
+      end
     end
 
   end
Set up expected state before test rather than cleaning after test.
Do chdir with a block so that it does not rely on the after hook.
