有人可以帮助一个新手吗? 我正在运行以下两个测试,试图最终获得相同的值。然而;当我通过computer_turn方法运行测试时,它给了我一个零值,但是当我直接通过player_first_turn函数运行测试时?它返回值:b2我正在寻找。前者为什么不给我:b2呢?
RSPEC测试。
before(:all) do
@test_case = ComputerLogic.new
$user_sign = "X"
$computer_name = "Watson"
$computer_sign = "O"
end
context "pertaining to computer_turn" do
it "should: if player picks corner then computer picks center" do
possible_places = {a1: "X",a2: @a2,a3: @a3,b1: @b1,b2: @b2,b3: @b3,c1: @c1,c2: @c2,c3: @c3}
@test_case.computer_turn(possible_places).should == :b2
end
end
context "pertaining to player_first_turn? and computer_first_move" do
it "should: if player picks corner then computer picks center" do
possible_places = {a1: "X",a2: @a2,a3: @a3,b1: @b1,b2: @b2,b3: @b3,c1: @c1,c2: @c2,c3: @c3}
@test_case.player_first_turn?(possible_places).should == :b2
end
我正在运行RSPEC测试的功能。
def computer_turn(possible_places)
if player_first_turn?(possible_places)
elsif attack
elsif counter_attack
elsif fork_play?
else random_move
end
end
def player_first_turn?(possible_places)
@first_turn = possible_places.select { |key, value| value == $user_sign }
if @first_turn.length == 1
return computer_first_move
else
return false
end
end
def computer_first_move
check_center = @first_turn.keys.first
if check_center == :b2
move = { a1: @a1,a3: @a3, c1: @c1,c3: @c3 }.keys.sample
#return declare_computer_move(move)
else
move = :b2
#return declare_computer_move(move)
end
end
答案 0 :(得分:0)
问题在于,如果computer_turn
是真实的,nil
会返回player_first_turn?(possible_places)
,因为if
表达式与后续elsif
之间没有代码。事实上,computer_move
将返回nil
,以防条件表达式的任何真实,否则它将返回random_move
。
if
语句永远不会返回它正在评估的条件的值。它返回条件表达式后面的语句系列或表达式的值,如果没有,则返回nil
。