On Feb 15, 2012, at 9:57 AM, Serguei Cambour wrote:

> I just tried to integrate a simple test to check some options passed as
> argument to a class to be executed:
> [code]
> require 'spec_helper'
> 
> module PropertyInjector
>  describe Options do
>    describe "#source folder option" do
>      it "sets up a source folder" do
>        argv = %w{-e path/to/some/folder}
>        puts "argv[0]: #{argv[0]}"
>        puts "argv[1]: #{argv[1]}"
>        options = Options.new(argv)
>        puts "options.export_folder: #{options.export_folder}"
>        options.export_folder.should == argv[1]
>      end
>    end
>  end
> end
> [/code]
> class Options:
> [code]
> module PropertyInjector
>  class Options
> def initialize(argv)
>      @bundle_folder = DEFAULT_BUNDLE_FOLDER
>      @export_folder = DEFAULT_EXPORT_FOLDER
>      @import_file_path = File.join(DEFAULT_IMPORT_FOLDER,
> EXCEL_FILE_NAME)
>      @export = true
>      @locales = ['en']
>      parse(argv)
>    end
> 
>    private
> def parse(argv)
>      OptionParser.new do |opts|
>        opts.banner = "Usage: translation properties injector
> [options]."
> 
>        opts.on("-e [EXPORT_FOLDER]", "--export [EXPORT_FOLDER]",
> String, "Path to the export folder to contain a generated Excel file,
> (default=#{DEFAULT_EXPORT_FOLDER}") do |folder|
>          @export = true
>          @export_folder = folder if folder
>        end
> 
>        opts.on("-i [EXCEL_FILE_PATH]", "--import [EXCEL_FILE_PATH]",
> String, "Path to the Excel file to import translations from
> (default=#{DEFAULT_IMPORT_FOLDER}/#{EXCEL_FILE_NAME})") do |file_path|
>          @export = false
>          @import_file_path = file_path if file_path
>        end
> 
>        opts.on("-b [BUNDLE_FOLDER]", "--bundle [BUNDLE_FOLDER]",
> String, "Path to the folder containing properties files,
> (default=#{DEFAULT_BUNDLE_FOLDER})") do |folder|
>          @bundle_folder = folder if folder
>        end
> 
>        opts.on("-l [l1,l2,l3]", "--langs [l1,l2,l3]", Array, "Locales
> values to use separated by comma, starting from the known one,
> default=en") do |langs|
>          if langs
>            @locales = langs.map(&:downcase)
>            @locales.uniq!
>          end
> 
>        end
> 
>        opts.on("-", "--help", "Show this message") do
>          puts opts
>          exit
>        end
> 
>        begin
>          argv = ["-h"]  if argv.empty?
>          opts.parse!(argv)
>        rescue OptionParser::ParseError => e
>          STDERR.puts e.message, "\n", opts
>          exit(1)
>        end
>      end
>    end
>  end
> end
> [/code]
> After running the test, here is what I got:
> [code]
> PropertyInjector::Options
>  #source folder option
> argv[0]: -e
> argv[1]: path/to/some/folder
> options.export_folder: path/to/some/folder
>    sets up a source folder (FAILED - 1)
> 
> Failures:
> 
>  1) PropertyInjector::Options#source folder option sets up a source
> folder
>     Failure/Error: options.export_folder.should == argv[1]
>       expected: nil
>            got: "path/to/some/folder" (using ==)
>     # ./spec/property_injector/options_spec.rb:12:in `block (3 levels)
> in <module:PropertyInjector>'
> 
> Finished in 0.01562 seconds
> 1 example, 1 failure
> 
> Failed examples:
> 
> rspec ./spec/property_injector/options_spec.rb:6 #
> PropertyInjector::Options#source folder option sets up a source folder
> [/code]
> WHY the second argument is NIL if it WAS NOT before
> (path/to/some/folder)?
> Thank you in advance.
> 
> -- 
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> rspec-users mailing list
> rspec-users@rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users

OptionParser is obviously mutating "argv". Try passing "argv.dup"

_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to