Testing a clean Ruby bin / my_app.rb application with RSpec?

I have a command line application (NON-RAILS) written in pure Ruby that I output through Cucumber and RSpec. This follows the typical hierarchy of lib, bin, spec and feature directory application hierarchies.

So far, I have been following the traditional process of writing a failed Cucumber function / script, resetting to RSpec to extract the supporting lib files, and then to transfer the script.

Unfortunately, this does not look like how to straighten the main entry point of the application in "bin / my_application.rb". The main problem for me is that I do not describe the class in RSpec, it is a serial Ruby script for managing application classes and initialization via command line parameters and parameters.

"bin / my_application.rb" is just a small shell for analyzing command line parameters and passing them to my main application class as initialization parameters. I would still like to check the behavior of the bin script (e.g. MyApp.should_receive (option_a) .with (parameter)).

Any suggestions / thoughts / tips? Is this a common test strategy for exile command line Ruby script behavior?

Thanks in advance.

+4
source share
1 answer

Not sure if I fully understand what you are asking for, but I would say that if you want to use RSpec to check for parameter passing, this should be easy enough to do. Let's say you have your wrapper script:

# my_application.rb command = Command.new command.foo = true if ARGV[0] command.bar = true if ARGV[1] command.baz = false if ARGV[2] command.make_dollars(1000000) 

Just compile it and make it a suitable class for testing.

 # command_runner.rb class CommandRunner def run(args, command = Command.new) command.foo = true if args[0] command.bar = true if args[1] command.baz = false if args[2] command.make_dollars(1000000) end end # my_application.rb CommandRunner.new.run(ARGV) 

Now the only thing you have not tested is your default parameter in the run command and one line in the my_application.rb file

Hope this helps. Brandon

+4
source

All Articles