# How do you use flexmock or mocha to test user interaction?

**URL:** <https://www.ruby-forum.com/t/how-do-you-use-flexmock-or-mocha-to-test-user-interaction/119915>\
**Category:** Ruby\
**Created:** [November 12, 2007, 6:05am UTC](https://www.ruby-forum.com/t/how-do-you-use-flexmock-or-mocha-to-test-user-interaction/119915 "2007-11-12T06:05:12Z")\
**Posts on this page:** 2\
**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 12, 2007, 6:05am UTC](https://www.ruby-forum.com/t/how-do-you-use-flexmock-or-mocha-to-test-user-interaction/119915/1 "2007-11-12T06:05:12Z")

</div>

I can’t seem to figure this out

Here’s my code that takes user input

def input\_money (machine)  
print ‘How much money would you like to put in?’  
money\_used = gets.chomp.to\_f  
#the check is not working  
if money\_used \> @money  
puts ‘You do not have that much’  
else  
@money = @money - money\_used  
machine.money\_used = money\_used  
puts 'You insert ’ + money\_used.to\_s  
end  
end

My test:

def test\_money\_back #this checks to see if you get the right amount of  
money  
poo = Vending\_Machine.new  
bob = Person.new  
bob.input\_money(poo)  
flexmock($stdin){|mock| mock.should\_receive(“gets”).and\_return(“2”)}  
# I know this is wrong!  
expected = poo.money\_used #one of the instance variables

```
assert_equal 2, expected

```

end

Right now, since the regular unit test doesn’t take any inputs, the  
money\_used is always going to be 0. How do I make a mock that sets the  
value of money\_used to 2?

I found in a different thread to use this:  
flexmock($stdin){|mock| mock.should\_receive(“gets”).and\_return(“junk”)}

but how do you tell it which method the gets is suppose to be set as 2?  
Is there some way to replace the real gets with the mock gets?

Been reading the forum and searching on google the last 2 hours, can’t  
figure out how this works!

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 12, 2007, 3:54pm UTC](https://www.ruby-forum.com/t/how-do-you-use-flexmock-or-mocha-to-test-user-interaction/119915/2 "2007-11-12T15:54:44Z")

</div>

I’m going to give you the answer, but I’m not giving it to you in  
code. it’s too obvious. you need to think about how flexmock works. it  
attaches methods to objects and guarantees their return value. the  
code you copy-pasted, you should read it and figure out what it means.  
it’s attaching a method to $stdin and guaranteeing the method will  
return the string “junk”. so attach a method to $stdin which returns 2  
instead, and then call that method on $stdin.

by the way, you used the variable name “poo” - in English that’s a  
word that children use for feces. I definitely recommend a different  
variable name. either “foo” or “shit.” or, if it’s a transliteration  
of an Asian name, use “pu” instead. nobody wants to be called poo.

–  
Giles B.

Blog: [http://gilesbowkett.blogspot.com](http://gilesbowkett.blogspot.com)  
Portfolio: [http://www.gilesgoatboy.org](http://www.gilesgoatboy.org)  
Tumblelog: [http://giles.tumblr.com/](http://giles.tumblr.com/)
