【发布时间】:2015-11-24 17:50:56
【问题描述】:
我已经尝试了两天来测试一个模拟电梯的程序,但收效甚微。这是我的电梯课程,该程序仍在进行中,我还注释掉了一些对我遇到问题的测试可能不是必需的方法。如果您认为需要,我很乐意展示更多代码
class Elevator
attr_accessor :current_floor
GROUND = 0
TOP = 15
def initialize
@floors = [] # list of floors to travel to
@pending = [] # store floors not in direction of travel
@current_floor = GROUND
@going_up = true # cannot travel downward from ground floor
@going_down = false
end
def get_input
gets.chomp
end
def run
enter_floors
sort_floors
move_to_floor
end
def enter_floors
# prompts the user for input and calls check_floor_numbers
end
def check_floor_numbers floors
# calls validate_floors to ensure user entered '2' instead of 'two'
# if proper floor numbers have been entered this method adds the number
# to @floors array other wise it calls floor_error_message
end
def floor_error_message
puts "Please enter numbers only."
enter_floors
end
def sort_floors
# if we are on the ground floor this method sorts @floors in ascending order
# if we are on the top floor it sorts @floors in descending order
# else it calls check_direction_of_travel
end
def move_to_floor
floor = @floors[0]
if @current_floor == floor
puts "You are already on floor #{floor}"
else
print_direction
(@current_floor..floor).each { |floor| puts "...#{floor}" }
@current_floor = floor # update current_floor
@floors.delete_at(0) # remove floor from list
end
check_for_more_passengers
end
def check_for_more_passengers
puts "Are there any more passengers? (Y/N)"
answer = (get_input).upcase
answer == 'Y' ? run : check_next_move
end
def check_next_move
if @floors.empty? && @pending.empty?
end_ride
else
move_to_floor
end
end
def check_direction_of_travel
# not implemented - add floor to appropriate array depending on dir
# of travel
end
def end_ride
puts "\n\nEND."
end
def print_direction
msg = " "
@going_up ? msg = "Going Up!" : msg = "Going Down!"
puts msg
end
end
我正在尝试测试电梯是否可以移动到特定楼层。起初,我在不运行程序本身的情况下测试来自控制台的输入时遇到了麻烦。我asked a question about this 并被推荐给this answer in a different question。有问题的答案将gets.chomp 提取到一个单独的方法,然后覆盖测试中的方法。我最终得到了这样的结果:
describe "it can move to a floor" do
before do
@@moves = ["2", "N"]
def get_input; @@moves.next end
end
it "should move to floor 2" do
e = Elevator.new
e.run
assert_equal(e.current_floor, 2)
end
end
问题: get_input 未被正确覆盖,运行测试套件提示用户输入,因此建议我 open the Elevator class in the test itself 以确保该方法被正确覆盖。尝试这样做最终导致了这样的测试:
describe "it can move to a floor" do
before do
class Elevator
attr_accessor :current_floor
@@moves = ["2", "N"]
def get_input; @@moves.next end
def run; end
end
end
it "should move to floor 2" do
e = Elevator.new
e.run
assert_equal(e.current_floor, 2)
end
end
我不得不覆盖 run 并为 current_floor 添加一个 attr_accessor,因为我遇到了方法缺失错误。
问题:此测试出现以下错误:
1) 失败:它可以移动到一个楼层#test_0001_should move to floor 2 [elevator_test.rb:24]:预期:无实际:2
我尝试尽可能地整理 Elevator 类,并在给定程序参数的情况下使方法尽可能简单。
谁能指出我解决这个问题的正确方向,也许是伪代码示例(如果可能的话)来演示如果答案是重构,我应该如何解决这个问题。
请记住,我还想实施其他测试,例如检查电梯类是否可以维护楼层列表,或者它是否可以改变方向,当您将来回答时。
【问题讨论】:
-
@floors是一个空数组,您的代码使用@floors[0]恰好为 nil。如上所示您的代码给出错误ArgumentError: bad value for range -
电梯课对我来说运行良好。你想让我把整个未注释的类放到 github 上吗?
-
如果你没问题,你可以分享它 - 并在上面的问题中放置一个链接。
-
gist.github.com/sheekap/730088ddef85511dbd3e(@floors 在 check_floor_number 方法中填充)