[rspec-users] Inspect STDERR in a story

Edvard Majakari edvard at majakari.net
Sat Apr 12 06:51:10 EDT 2008

On Sat, Apr 12, 2008 at 11:54 AM, Ashley Moran
<ashley.moran at patchspace.co.uk> wrote:
> Hi
>  I have a story step that looks like this:
>    When /(the user|then) runs "migrate (.*)"/ do |_, args|
>      cd project_dir do
>        @output = `#{migrate} #{args}`
>      end
>        @output_lines = @output.split("\n")
>    end
>  Which is fine for testing STDOUT but not STDERR.  I don't want to
>  redirect STDERR to STDOUT because then the story can't prove that
>  error messages are displayed separately (although the specs would)

How about something along the lines

  cd project_dir do
     @stdout, @stdterr = capture_outputs("#{migrate} #{args}")

  def capture_outputs(cmd)
     stderr_log = "/tmp" + "/#{$0}-{$$}.log"
     out = `cmd 2> #{stderr_log}`
     err = File.read(stderr_log) if File.exist? stderr_log
     [out, err]

Totally untested, but hopefully you get the idea?

"One day, when he was naughty, Mr Bunnsy looked over the hedge into
Farmer Fred's field and it was full of fresh green lettuces. Mr
Bunnsy, however, was not full of lettuces. This did not seem fair."
 -- Terry Pratchett, Mr. Bunnsy Has An Adventure

More information about the rspec-users mailing list