From: "Shot (Piotr Szotkowski)" Date: 2009-10-15T07:08:16+09:00 Subject: Re: Testing command-line apps with Cucumber: advice? --Kj7319i9nmIyA2yE Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable James Coglan: > I'd like to write some Cucumber features > for a command-line app I'm working on [=E2=80=A6] > 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=E2=80=99t test) that simply calls Executable.new.run a= nd 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= =2Erb http://github.com/Chastell/art-decomp/blob/master/spec/art-decomp/executabl= e_spec.rb As you can see, Executable#initialize=E2=80=99s 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= =2Erb Executable#run=E2=80=99s params default to $stdout and $stdin, but the spec= uses StringIOs in their place to be able to =E2=80=98read from=E2=80=99 and =E2= =80=98write to=E2=80=99 the Executable. =E2=80=94 Shot --=20 =E2=80=98I finally made a difference at work.=E2=80=99 =E2=80=98How many victims?=E2=80=99 [Dilbert] --Kj7319i9nmIyA2yE Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAkrWS8sACgkQi/mCfdEo8UoR1QCfbPEk8Gn4KUgCz0s/gkd3lZAh VDYAoMKM9hzL/5KH4xMaavz4pjIq07ok =gmoF -----END PGP SIGNATURE----- --Kj7319i9nmIyA2yE--