我有班级游戏和班级游戏的红宝石代码,我在网球卡塔工作。
class Player
def score
if @player_one.points <3 and @player_two.points<3
"#{@player_one.score} #{@player_two.score}"
elsif @player_one.points == @player_two.points
"#{@player_one.score} iguales"
end
if @player_one.points == 3 and @player_two.points == 3
'deuce'
elsif (@player_one.points == @player_two.points + 1) and (@player_two.points >=3)
"Ventaja #{@player_one.name}"
elsif (@player_one.points >= 4) and (@player_one.points >= (@player_two.points + 2))
"Game #{@player_one.name}"
end
end
end
这个测试无法通过,所有已经通过的其他测试都不在这里。
it 'return 30-iguales when both players have 2 points scored' do
player_one = Player.new
player_two = Player.new
game = TennisGame.new(player_one, player_two)
player_one.wins_point
player_one.wins_point
player_two.wins_point
player_two.wins_point
expect(game.score).to eq("30 iguales")
end
我得到的错误是预期“30 iguales”但得到nil
。
我所有的其他测试都已通过,但是在这个测试失败了,我无法解决问题。
答案 0 :(得分:0)
即使没有明确的return
语句,Ruby方法也会返回最后一个已计算的表达式,我认为这就是你要去的地方。问题是,对于测试数据,该方法将评估"#{@player_one.score} iguales"
,但随后继续评估第二个if
语句,并一直通过它返回nil
。
您需要修复的选项是:
if
语句而不是两个。或者: