Unable to print STDOUT Ruby

26 views
Skip to first unread message

Laurence Rosenzweig

unread,
Jun 24, 2015, 9:14:58 AM6/24/15
to rs...@googlegroups.com

I have been running Rspec tests and want to print out STDOUT. It seems like a very simple problem yet I see very little documentation about this issue and I have been unable to figure out how to print it. I know as a variable you can do $stdout but whenever I print it (using pp, puts, or print), I cannot get the actual value of stdout.

 

I attached an image of one of my tests with a few of my different attempts (commented out at the moment).

 

Thanks for your help!

Laurence Rosenzweig

unread,
Jun 24, 2015, 9:18:02 AM6/24/15
to rs...@googlegroups.com
Screen Shot 2015-06-23 at 2.07.56 PM.png

Myron Marston

unread,
Jun 24, 2015, 1:42:57 PM6/24/15
to rs...@googlegroups.com, lar...@cornell.edu
Ruby's `$stdout` doesn't provide you with what has been written to stdout.  It's an IO object that can be written to but not read from.  If you want to set an expectation about what has been written to stdout, we have a matcher for that:


HTH,
Myron

 

Laurence Rosenzweig

unread,
Jun 24, 2015, 2:49:33 PM6/24/15
to rs...@googlegroups.com, lar...@cornell.edu
Thanks for the reply. I had seen that a few days ago but didn't think it was helpful...perhaps I did not use it correctly. It felt like whatever I wrote, it just approved. Basically I have a command I am running and I want to see what it's stdout is to see for some of my tests why it is not matching my expectation. See attached image for what I am trying to do.
Screen Shot 2015-06-24 at 2.47.25 PM.png

Myron Marston

unread,
Jun 24, 2015, 2:53:07 PM6/24/15
to rs...@googlegroups.com, lar...@cornell.edu
Please provide code inline or via links to gist.github.com (or a similar service) rather than providing photos.

Anyhow, the `command` method isn't part of core RSpec.  Whatever library you are using that provides that probably provides a way to get the stdout of running that command.  You'll have to check with the docs or project maintainers on that.

Myron

--
You received this message because you are subscribed to the Google Groups "rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rspec+un...@googlegroups.com.
To post to this group, send email to rs...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rspec/f2b776fa-011b-4ddb-ba37-0be4025d3e85%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Alex Chaffee

unread,
Jun 25, 2015, 3:27:08 AM6/25/15
to rs...@googlegroups.com
Try something like

it "works" do
  output = `firefox -v`
  expect(output).to eq("1.2.3")
end

There are other ways in Ruby to get the output of shell commands -- look up popen3 and popen4 -- but backtick is the simplest. Of course, it's platform-dependent and YMMV and all that.

 - A




For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages