# How do you write a unit test of methods that needs user inpu

**URL:** <https://www.ruby-forum.com/t/how-do-you-write-a-unit-test-of-methods-that-needs-user-inpu/119159>\
**Category:** Ruby\
**Created:** [November 4, 2007, 12:52am UTC](https://www.ruby-forum.com/t/how-do-you-write-a-unit-test-of-methods-that-needs-user-inpu/119159 "2007-11-04T00:52:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [November 4, 2007, 12:52am UTC](https://www.ruby-forum.com/t/how-do-you-write-a-unit-test-of-methods-that-needs-user-inpu/119159/1 "2007-11-04T00:52:42Z")

</div>

Will I need to change my code or is there a way for me to write my unit  
test that automatically enters some inputs?

Idealy, I’d like to do it so that when I run my unit test, It’ll ask me  
for the inputs. Right now it just goes thru the code without asking me  
for the inputs.

THanks

---

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [November 4, 2007, 1:01am UTC](https://www.ruby-forum.com/t/how-do-you-write-a-unit-test-of-methods-that-needs-user-inpu/119159/2 "2007-11-04T01:01:11Z")

</div>

pood.forums wrote:

> Will I need to change my code or is there a way for me to write my unit  
> test that automatically enters some inputs?

Get with Mocha, a mock-object library. Then mock the method that returns  
user input, and put whatever the test needs into it.

> Idealy, I’d like to do it so that when I run my unit test, It’ll ask me  
> for the inputs. Right now it just goes thru the code without asking me  
> for the inputs.

Okay, this is different. If it’s interative, it’s not an automated test.  
If  
you to test many different inputs, put them into a table and loop over  
the  
table each time you test.

Make the table easy to edit, such as a YAML file.

See “FIT” and “Fitnesse” for good GUIs on these tables of test data.  
Then  
invent your own, Ruby-style, without devolving into FIT’s sounce code!

---

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [November 4, 2007, 8:31am UTC](https://www.ruby-forum.com/t/how-do-you-write-a-unit-test-of-methods-that-needs-user-inpu/119159/3 "2007-11-04T08:31:10Z")

</div>

Feng T. wrote:

> Will I need to change my code or is there a way for me to write my unit  
> test that automatically enters some inputs?

It’s somewhat difficult. This works for me:

## my\_program.rb:

def get\_input  
while true  
print "Enter a non-zero integer greater than -5: "  
num = gets

```
num_int = num.to_i
num_float = num.to_f

if num_int != num_float
  puts "You entered a float. Try again."
elsif num_int <= -5
  puts "You entered a number that was too small."
elsif num_int == 0
  puts "You entered 0 or a word. That is not allowed."
else
  return num_int
end

```

end  
end

if **FILE** == $0  
result = get\_input  
puts “You entered #{result}. Thank you.”  
end

## my\_program\_UnitTest.rb

require ‘my\_program’  
require ‘test/unit’  
require ‘stringio’

class TestMyProgram \< Test::Unit::TestCase

def setup  
$stdin = DATA #DATA is like a file containing the stuff after  
**END**  
trash\_can = StringIO.new  
$stdout = trash\_can

```
#If you want to see the prompts shown to the user as part of
#the unit test output and/or be able to send output to your
#terminal for debugging, comment out the previous line.

```

end

def teardown  
$stdin = STDIN  
$stdout = STDOUT  
end

def test\_get\_input  
while not DATA.eof?  
num = get\_input

```
    num_int = num.to_i
    num_float = num.to_f

    assert_not_equal(0, num_int)
    assert(num_int > -5, "The number was not greater than -5.")
    assert_equal(num_int, num_float)
  end

```

end  
end

**END**  
-6  
-4  
0  
2.5  
6 #Must end the data with a good number–otherwise get\_input will  
#run out of data, causing gets in get\_input to return nil, which  
will  
#cause get\_input to go into an infinite loop.

> Idealy, I’d like to do it so that when I run my unit test, It’ll ask me  
> for the inputs.

After typing in 5 - 10 different numbers into the command line for hours  
on end, you will probably think differently about testing code by hand.  
In fact, if you are going to test code by hand, you don’t need unit  
testing. Unit testing is designed to enable you to test your program by  
driving it with another program. That allows you to do tests relatively  
quickly and thoroughly–and the tests are repeatable.
