Creating a command-line app in ruby might be fun, being able to test them is just as important as testing any other web application. But you can do this with Aruba. Aruba is a Cucumber extension for testing command-line applications written in any language.
Passing arguments, interacting with the file system, capturing exist codes en mimicking interactive usage are all features provided out of the box.
Add Aruba to your .gemspec file and run bundle
... spec.add_development_dependency("aruba") ...
$ bundle install
Now open your Rakefile and add the cucumber rake task
This will make the features rake task available
$ rake -T rake build # Build my_gem-0.0.1.gem into the pkg directory. rake features # Run Cucumber features rake install # Build and install my_gem-0.0.1.gem into system gems. rake release # Create tag v0.0.1 and build and push my_gem-0.0.1.gem to Rubygems
Now require auruba in your env.rb file under features/support
Now you are all set to start using Aruba.
In my previous post in how to create a ruby command-line app, I created a simple hello command that took a name as parameter and displayed it on the screen. So let’s write a small test for this. A test might look like this:
Feature: Display the given parameter name with a hardy Hello greeting Scenario: Greet the user with a given name When I successfully run `my_gem hello michael` Then the output should contain "Hello michael"
So this feature just tests the fact that when you run the command my_gem hello michael, you get the right output back from your command-line app.
I find using a BDD ( behavior-driven development ) approach for testing command-line applications the best way to go. Especially when you want to test the cli interactions.