[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

redirecting stdout to a String

Mark Volkmann

3/24/2006 1:51:00 PM

I wrote a unit test for a method that writes to stdout. I got it to
work, but I'm not sure my approach was the best. Is there a more
recommended way to do this?

# Create a String with a singleton write method
# that allows it to take the place of stdout.
actual = '''
def actual.write(data); self << data; end
old_stdout, $stdout = $stdout, actual

# Invoke method to test that writes to stdout.

$stdout = old_stdout # restore stdout

--
R. Mark Volkmann
Object Computing, Inc.


3 Answers

James Gray

3/24/2006 1:57:00 PM

0

On Mar 24, 2006, at 7:50 AM, Mark Volkmann wrote:

> I wrote a unit test for a method that writes to stdout. I got it to
> work, but I'm not sure my approach was the best. Is there a more
> recommended way to do this?
>
> # Create a String with a singleton write method
> # that allows it to take the place of stdout.
> actual = '''
> def actual.write(data); self << data; end

See the standard library, StringIO.

James Edward Gray II


Mark Volkmann

3/24/2006 2:12:00 PM

0

On 3/24/06, James Edward Gray II <james@grayproductions.net> wrote:
> On Mar 24, 2006, at 7:50 AM, Mark Volkmann wrote:
>
> > I wrote a unit test for a method that writes to stdout. I got it to
> > work, but I'm not sure my approach was the best. Is there a more
> > recommended way to do this?
> >
> > # Create a String with a singleton write method
> > # that allows it to take the place of stdout.
> > actual = '''
> > def actual.write(data); self << data; end
>
> See the standard library, StringIO.

Thanks! I have that working, but it's not clear to me how that is
better. In fact, it seems to take more code to do it that way. I now
have this.

require 'stringio'

sio = StringIO.new
old_stdout, $stdout = $stdout, sio
# Invoke method to test that writes to stdout.
$stdout = old_stdout # restore stdout
actual = sio.string

Am I overcomplicating this?

--
R. Mark Volkmann
Object Computing, Inc.


James Gray

3/24/2006 4:07:00 PM

0

On Mar 24, 2006, at 8:12 AM, Mark Volkmann wrote:

> On 3/24/06, James Edward Gray II <james@grayproductions.net> wrote:
>> On Mar 24, 2006, at 7:50 AM, Mark Volkmann wrote:
>>
>>> I wrote a unit test for a method that writes to stdout. I got it to
>>> work, but I'm not sure my approach was the best. Is there a more
>>> recommended way to do this?
>>>
>>> # Create a String with a singleton write method
>>> # that allows it to take the place of stdout.
>>> actual = '''
>>> def actual.write(data); self << data; end
>>
>> See the standard library, StringIO.
>
> Thanks! I have that working, but it's not clear to me how that is
> better. In fact, it seems to take more code to do it that way.

You originally asked:

>>> Is there a more recommended way to do this?

I was just trying to answer that question.

I prefer the StringIO method because it is a full mock API, as Robert
Dober said. However, if you know you will only need the one method
(now and forever?!) there is probably little difference.

James Edward Gray II