loadyaml_spec.rb 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. require 'spec_helper'
  2. describe 'loadyaml' do
  3. it { is_expected.not_to eq(nil) }
  4. it { is_expected.to run.with_params.and_raise_error(ArgumentError, %r{wrong number of arguments}i) }
  5. context 'when a non-existing file is specified' do
  6. let(:filename) { '/tmp/doesnotexist' }
  7. before(:each) do
  8. File.expects(:exists?).with(filename).returns(false).once
  9. YAML.expects(:load_file).never
  10. end
  11. it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') }
  12. it { is_expected.to run.with_params(filename, 'đẽƒằưļŧ' => '٧ẵłựέ').and_return('đẽƒằưļŧ' => '٧ẵłựέ') }
  13. it { is_expected.to run.with_params(filename, 'デフォルト' => '値').and_return('デフォルト' => '値') }
  14. end
  15. context 'when an existing file is specified' do
  16. let(:filename) { '/tmp/doesexist' }
  17. let(:data) { { 'key' => 'value', 'ķęŷ' => 'νậŀųề', 'キー' => '値' } }
  18. before(:each) do
  19. File.expects(:exists?).with(filename).returns(true).once
  20. YAML.expects(:load_file).with(filename).returns(data).once
  21. end
  22. it { is_expected.to run.with_params(filename).and_return(data) }
  23. end
  24. context 'when the file could not be parsed' do
  25. let(:filename) { '/tmp/doesexist' }
  26. before(:each) do
  27. File.expects(:exists?).with(filename).returns(true).once
  28. YAML.stubs(:load_file).with(filename).once.raises StandardError, 'Something terrible have happened!'
  29. end
  30. it { is_expected.to run.with_params(filename, 'default' => 'value').and_return('default' => 'value') }
  31. end
  32. end