测试程序,该程序使用来自控制台的用户输入来运行 [英] Testing program that runs with user input from the console
问题描述
我正在为一个模拟电梯的系统编写测试.例如,我要测试电梯是否可以改变方向以及可以移动到指定楼层.
I'm writing tests for a system that models an elevator. For example, I want to test that the elevator can change direction and that it can move to a specified floor.
我有以下方法:
def initialize
@current_floor = 0
@requested_floor = 0
end
def get_value
gets.chomp
end
def arrival
print "Enter floor number: "
@requested_floor = get_value
# only proceed if user entered an integer
if validate_floor_number(@requested_floor)
@requested_floor = @requested_floor.to_i
move
else
arrival
end
end
def move
msg = ""
@current_floor < @requested_floor ? msg = "Going Up!" : msg = "Going Down"
puts msg
@current_floor = @requested_floor
next_move
end
def next_move
puts "Do you want to go to another floor? Y/N"
another_floor = (get_value).upcase
another_floor == 'N' ? final_destination : arrival
end
我通过调用Elevator.new.arrival
启动程序.要检查电梯是否改变了方向,我需要将@current_floor
的值存储在一个临时变量中,然后在调用move
后检查其值是否已更改.
I start the program by calling Elevator.new.arrival
. To check that the elevator has changed directions, I need to store the value of @current_floor
in a temporary variable then check it's value has changed after move
has been called.
I am testing input from the console using an IO pipe thanks to the answers in this question, but I'm not sure how to apply that knowledge to user interaction that's part of a method.
如何模拟通过move
方法从开始(Elevator.new.arrival
)开始运行的程序并在那里停止运行,所以我可以检查@current_floor
的值-所有这些都无需运行程序本身并使用IO管道来模拟用户交互?
How can I simulate the program running from the start (Elevator.new.arrival
) through the move
method and stop it there so I can check the value of @current_floor
- all of this without running the program itself and using the IO pipe to simulate user interaction?
我觉得我可能以错误的方式进行了程序设计.如果有人能指出正确的方向来解决这个问题,我将不胜感激.
I have a feeling that I might have gone about the design of the program in the wrong way. If anyone can even point me in the right direction towards solving this problem I'd appreciate it.
修改
根据Wand Maker的建议,我编写了如下测试:
According to the suggestions from Wand Maker I've written a test as follows:
describe "checks that the elevator can change directions" do
before do
moves = [3, 'Y', 5, 'Y', 2, 'Y', 7, 'N']
def get_value; moves.next end
end
it "should stop on floor 7" do
Elevator.new.arrival
assert_equal(@current_floor, 7)
end
end
不幸的是,当我运行测试文件时,该程序仍然运行并提示用户输入.也许我打错了arrival
的电话,但我想不出另一种方法.
Unfortunately, when I run my test file, the program still runs and prompts for user input. Maybe I'm calling arrival
incorrectly but I can't think of another way to do it.
推荐答案
如此答案所示,您可以覆盖getvalue
输入用户输入.
As demonstrated in this answer, you can override getvalue
to feed in the user input.
这里是完整的代码,无需实际使用gets
即可工作.我必须添加一些缺少的方法-validate_floor_number
和final_destination
:
Here is complete code that works without actually using gets
. I had to add couple of missing methods - validate_floor_number
and final_destination
:
require 'minitest/autorun'
class Elevator
attr_accessor :current_floor
def initialize
@current_floor = 0
@requested_floor = 0
#@last_floor = false
end
def get_value
gets.chomp
end
def validate_floor_number(v)
v.to_i rescue false
end
def arrival
print "Enter floor number: "
@requested_floor = get_value
# only proceed if user entered an integer
if validate_floor_number(@requested_floor)
@requested_floor = @requested_floor.to_i
move
else
arrival
end
end
def move
msg = ""
@current_floor < @requested_floor ? msg = "Going Up!" : msg = "Going Down"
puts msg
@current_floor = @requested_floor
next_move
end
def final_destination
puts "Reached your floor"
end
def next_move
puts "Do you want to go to another floor? Y/N"
another_floor = (get_value).upcase
another_floor == 'N' ? final_destination : arrival
end
end
describe "checks that the elevator can change directions" do
before do
class Elevator
@@moves = [3, 'Y', 5, 'Y', 2, 'Y', 7, 'N'].each
def get_value; @@moves.next end
end
end
it "should stop on floor 7" do
e = Elevator.new
e.arrival
assert_equal(e.current_floor, 7)
end
end
以上程序的输出:
Run options: --seed 2561
# Running:
Enter floor number: Going Up!
Do you want to go to another floor? Y/N
Enter floor number: Going Up!
Do you want to go to another floor? Y/N
Enter floor number: Going Down
Do you want to go to another floor? Y/N
Enter floor number: Going Up!
Do you want to go to another floor? Y/N
Reached your floor
.
Finished in 0.001334s, 749.4982 runs/s, 749.4982 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
[Finished in 0.3s]
这篇关于测试程序,该程序使用来自控制台的用户输入来运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!