Hi all,
I’d like to write some Cucumber features for a command-line app I’m
working
on (specifically, 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
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:
http://github.com/Chastell/art-decomp/blob/master/bin/art-decomp
http://github.com/Chastell/art-decomp/blob/master/lib/art-decomp/executable.rb
http://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:
http://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/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