F
Feng Tien
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!
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!