# Testing command-line apps with Cucumber: advice?

**URL:** <https://www.ruby-forum.com/t/testing-command-line-apps-with-cucumber-advice/176620>\
**Category:** Ruby\
**Created:** [October 3, 2009, 12:02pm UTC](https://www.ruby-forum.com/t/testing-command-line-apps-with-cucumber-advice/176620 "2009-10-03T12:02:04Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![james-calivar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@james-calivar](https://www.ruby-forum.com/u/james-calivar)\
**Post date:** [October 3, 2009, 12:02pm UTC](https://www.ruby-forum.com/t/testing-command-line-apps-with-cucumber-advice/176620/1 "2009-10-03T12:02:04Z")

</div>

Hi all,

I’d like to write some Cucumber features for a command-line app I’m  
working  
on (specifically, [GitHub - jcoglan/claw: Command-line tool for searching and opening files](http://github.com/jcoglan/claw)). The app runs a  
Readline  
loop taking user input, and uses this input to search for and open files  
by  
running shell commands (grep, gedit, firefox etc.). Right now it’s using  
#puts and the backtick (`) method for output and shell commands, and I’m  
not  
sure how to go about testing the app’s output. Do I mock Kernel#puts,  
should  
I give the app an IO object to write output to so I can collect it, or  
soemthing else entirely?

If anyone has advice about how to structure this I’d be most grateful.

Cheers,  
James

---

<div class="post-metadata">

**Author:** ![james-calivar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/2bfe46/32.png) [@james-calivar](https://www.ruby-forum.com/u/james-calivar)\
**Post date:** [October 15, 2009, 12:08am UTC](https://www.ruby-forum.com/t/testing-command-line-apps-with-cucumber-advice/176620/2 "2009-10-15T00:08:58Z")

</div>

James C.:

> I’d like to write some Cucumber features  
> for a command-line app I’m working on [â€¦]

> Do I mock Kernel#puts, should I give the app an IO object to  
> write output to so I can collect it, or soemthing else entirely?

When I was faced with the same problem (albeit with RSpec rather than  
Cucumber, but I doubt it matters here) I ended up with a very thin  
wrapper (which I donâ€™t test) that simply calls Executable.new.run and  
then test the Executable with hand-crafted params and StringIOs:

> <https://github.com/Chastell/art-decomp/blob/master/bin/art-decomp>

> <https://github.com/Chastell/art-decomp/blob/master/lib/art-decomp/executable.rb>

> <https://github.com/Chastell/art-decomp/blob/master/spec/art-decomp/executable_spec.rb>

As you can see, Executable#initializeâ€™s param defaults to ARGV and  
the spec redirects $stderr to a StringIO object to inspect it for the  
desired errors.

The same with my other command line  
app, this time with input and output:

> <https://github.com/Chastell/signore/blob/master/bin/signore>

[http://github.com/Chastell/signore/blob/master/lib/signore/executable.rb](http://github.com/Chastell/signore/blob/master/lib/signore/executable.rb)  
[http://github.com/Chastell/signore/blob/master/spec/signore/executable\_spec.rb](http://github.com/Chastell/signore/blob/master/spec/signore/executable_spec.rb)

Executable#runâ€™s params default to $stdout and $stdin, but the spec uses  
StringIOs in their place to be able to â€˜read fromâ€™ and â€˜write toâ€™ the  
Executable.

â€” Shot
