Forum: RSpec rspec and testing IO

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Justin Beck (Guest)
on 2011-09-09 01:15
(Received via mailing list)
I have a class that expects some input via 'gets'.  I found that, when
testing, I can do something as simple as this:

@foo.stub!(:gets) { "stuff\n" }

However that only works in the most simple case.  In my case the class
asks a series of questions that require a yes/no answer culminating in
some end state.  The class then asks if the user would like to go
through the questions again and waits for a response of yes/no.  If I
want to test a single run through the questions (answering 'yes' to all
of them) and I stub 'gets' to always reply with 'yes' then I have no way
of breaking out of the loop.  The test I'm running dies and complains of
the stack being too deep.

So, my question is this:

How do folks typically go about testing classes that take input from
stdin?  I can post more code if necessary but this is a pretty simple
question to which, I'm sure, there is a reasonable solution.


Justin Ko (Guest)
on 2011-09-09 03:08
(Received via mailing list)
On Thu, Sep 8, 2011 at 3:03 PM, Justin Beck <> wrote:

> 'gets' to always reply with 'yes' then I have no way of breaking out of the
> Justin
> _______________________________________________
> rspec-users mailing list

Two ways to approach this. On the unit level, you can stub "gets" and
a different value each time:

stubs(:gets).and_return('yes', 'no', 'yes', 'yes', 'no')

Each time "gets" is called it will return the next value.

On a more high level, I would recommend using the aruba gem with RSpec.
is how I've done it in my own code:

Let us know if any of that works.
This topic is locked and can not be replied to.