[rspec-users] rspec and testing IO

Justin Beck justinbeck at mac.com
Thu Sep 8 22:10:17 EDT 2011


Thanks Justin, that'll work just fine for my simple case and, in the mean time, I'll look in to aruba also.

Much appreciated!

Justin

On Sep 8, 2011, at 7:38 PM, rspec-users-request at rubyforge.org wrote:
> 
> ------------------------------
> 
> Message: 8
> Date: Thu, 08 Sep 2011 17:03:50 -0400
> From: Justin Beck <justinbeck at mac.com>
> To: rspec-users at rubyforge.org
> Subject: [rspec-users] rspec and testing IO
> Message-ID: <50E4D18C-3F36-4707-82F1-6166AFB77000 at mac.com>
> Content-Type: text/plain; CHARSET=US-ASCII
> 
> 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.
> 
> Thanks!
> 
> Justin
> 
> 
> ------------------------------
> 
> Message: 9
> Date: Thu, 8 Sep 2011 17:37:42 -0600
> From: Justin Ko <jko170 at gmail.com>
> To: rspec-users <rspec-users at rubyforge.org>
> Subject: Re: [rspec-users] rspec and testing IO
> Message-ID:
> 	<CAJkeazJneskYdbTW0c-3yM2XxQ5gxmoDYOaS-Ch+rO9VMpweuw at mail.gmail.com>
> Content-Type: text/plain; charset="iso-8859-1"
> 
> On Thu, Sep 8, 2011 at 3:03 PM, Justin Beck <justinbeck at mac.com> wrote:
> 
>> 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.
>> 
>> Thanks!
>> 
>> Justin
>> _______________________________________________
>> rspec-users mailing list
>> rspec-users at rubyforge.org
>> http://rubyforge.org/mailman/listinfo/rspec-users
> 
> 
> Two ways to approach this. On the unit level, you can stub "gets" and return
> 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. Here
> is how I've done it in my own code:
> 
> https://github.com/justinko/reverse-polish-calculator/tree/master/spec/acceptance
> 
> Let us know if any of that works.
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20110908/78a4b9a6/attachment.html>
> 
> ------------------------------
> 
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
> 
> End of rspec-users Digest, Vol 63, Issue 6
> ******************************************



More information about the rspec-users mailing list