# Strategy for unit testing user input?

**URL:** <https://www.ruby-forum.com/t/strategy-for-unit-testing-user-input/116292>\
**Category:** Ruby\
**Created:** [October 4, 2007, 6:22am UTC](https://www.ruby-forum.com/t/strategy-for-unit-testing-user-input/116292 "2007-10-04T06:22:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [October 4, 2007, 6:22am UTC](https://www.ruby-forum.com/t/strategy-for-unit-testing-user-input/116292/1 "2007-10-04T06:22:02Z")

</div>

Hi,

I have a program that prompts the user on the command line to enter some  
data. What are some ways to unit test the method that obtains the  
input?

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [October 13, 2007, 1:06am UTC](https://www.ruby-forum.com/t/strategy-for-unit-testing-user-input/116292/2 "2007-10-13T01:06:14Z")

</div>

7stud – wrote:

> Hi,
> 
> I have a program that prompts the user on the command line to enter some  
> data. What are some ways to unit test the method that obtains the  
> input?

I’m not sure if you found an answer to this. I came across your post  
when I was looking for an answer to the same thing. The best way I  
found is to use flexmock (or other mock tool).

flexmock($stdout){|mock| mock.should\_receive(“puts”).with(“enter  
something”)}  
flexmock($stdin){|mock| mock.should\_receive(“gets”).and\_return(“junk”)}

---

<div class="post-metadata">

**Author:** ![7stud](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/7/57b2e6/32.png) [@7stud](https://www.ruby-forum.com/u/7stud)\
**Post date:** [October 13, 2007, 5:19pm UTC](https://www.ruby-forum.com/t/strategy-for-unit-testing-user-input/116292/3 "2007-10-13T17:19:17Z")

</div>

Kurt Walker wrote:

> 7stud – wrote:
> 
> > Hi,
> > 
> > I have a program that prompts the user on the command line to enter some  
> > data. What are some ways to unit test the method that obtains the  
> > input?
> 
> I’m not sure if you found an answer to this. I came across your post  
> when I was looking for an answer to the same thing. The best way I  
> found is to use flexmock (or other mock tool).
> 
> flexmock($stdout){|mock| mock.should\_receive(“puts”).with(“enter  
> something”)}  
> flexmock($stdin){|mock| mock.should\_receive(“gets”).and\_return(“junk”)}

Thanks.
