require 'spec_helper' require 'file_creator' RSpec.describe FileCreator do describe '#create' do subject(:create) { FileCreator.new.create(file_name) } let(:file_name) { "pluginscan.scan" } it 'tries to create a file with the given name' do allow(File).to receive(:new) create expect(File).to have_received(:new).with(file_name, 'w') end it 'returns the file if it successfully created it' do file = double(File) allow(File).to receive(:new).and_return file expect(create).to eq file end context 'if the filename is not a writeable location' do before { allow(File).to receive(:new).and_raise(Errno::EACCES) } it 'raises an error' do expect{ create }.to raise_error(FileCreator::Error, "You do not have permission to write to that location (#{file_name})") end # Real example: # File.new('/etc/foo', 'w') # => Errno::EACCES: Permission denied @ rb_sysopen - /etc/foo end context 'if the filename is a directory name' do before { allow(File).to receive(:new).and_raise(Errno::EISDIR) } it 'raises an error' do expect{ create }.to raise_error(FileCreator::Error, "File name is a directory (#{file_name})") end # Real example: # File.new("/", 'w') # => Errno::EISDIR: Is a directory @ rb_sysopen - / end context 'if the filename is in a directory which does not exist' do before { allow(File).to receive(:new).and_raise(Errno::ENOTDIR) } it 'raises an error' do expect{ create }.to raise_error(FileCreator::Error, "File name refers to a directory which does not exist (#{file_name})") end # Real example: # File.new("foo/bar", 'w') # => Errno::ENOTDIR: Not a directory @ rb_sysopen - foo/bar end end end