James Gray
3/24/2006 4:07:00 PM
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