-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfaking_gets_in.rb
More file actions
59 lines (48 loc) · 1.31 KB
/
Copy pathfaking_gets_in.rb
File metadata and controls
59 lines (48 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
require "minitest/autorun"
class InputFaker
def initialize(strings)
@strings = strings
end
def gets
next_string = @strings.shift
# Uncomment the following line if you'd like to see the faked $stdin#gets
puts "(DEBUG) Faking #gets with: #{next_string}"
next_string
end
def self.with_fake_input(strings)
$stdin = new(strings)
yield
ensure
$stdin = STDIN
end
end
class Waiter
attr_accessor :orders
attr_reader :number_of_diners
def initialize(number_of_diners)
@number_of_diners = number_of_diners
end
def take_orders!
self.orders = {}
number_of_diners.times do |n|
puts "Hello, diner. What is your name?"
name = gets
puts "Please to meet you. #{name}, what would you like for dinner?"
order = gets
self.orders[name] = order
end
end
end
class WaiterTest < Minitest::Test
def test_ordering
InputFaker.with_fake_input(["Harry", "The tarte tomate",
"Sally", "The pulled pork sandwich"]) do
waiter = Waiter.new(2)
waiter.take_orders!
assert waiter.orders.keys.include?("Harry")
assert waiter.orders.keys.include?("Sally")
assert_equal "The tarte tomate", waiter.orders["Harry"]
assert_equal "The pulled pork sandwich", waiter.orders["Sally"]
end
end
end