Ruby 理解rspec测试有困难
我的任务是用ruby编写代码以满足特定rspec测试的要求,但我确实很难理解这一点:Ruby 理解rspec测试有困难,ruby,rspec,Ruby,Rspec,我的任务是用ruby编写代码以满足特定rspec测试的要求,但我确实很难理解这一点: it "should check for a winner with top row" do @game.winner = nil @game.clearmatrix @game.setmatrixvalue(0, "0") @game.setmatrixvalue(1, "0") @ga
it "should check for a winner with top row" do
@game.winner = nil
@game.clearmatrix
@game.setmatrixvalue(0, "0")
@game.setmatrixvalue(1, "0")
@game.setmatrixvalue(2, "0")
@game.checkwinner.should == check_winner && @game.winner.should_not == nil
end
该计划将是一个零和交叉(tic-tac-toe?)游戏,上面的测试专门检查某人是否在最上面一行有所有3个空格(有各种其他测试来检查其他组合)
现在,我假设我应该创建一个名为winner的新方法,它应该有一些默认值
我不明白为什么我想调用clearmatrix作为checkwinner方法的一部分。。。当然,如果我清除矩阵,那么程序将无法检查是否有赢家
测试的最后一行是我很难理解的主要部分。我认为我需要在checkwinner中有一个if语句,如果所有3个空格都相同,它将返回true,但是check_winner是什么呢?这应该是另一种方法吗?为什么我要设置winner=nil,然后需要winner不是nil
这是我迄今为止对其他测试(所有通过)的帮助:
def start
#Calls method to display appropriate messages.
messages
end
def created_by
return "myname"
end
def student_id
return mystudentid #this is an integer
end
def messages
@output.puts "Welcome to Noughts and Crosses!"
@output.puts "Created by:Stephen Mitchell"
@output.puts "Starting game..."
@output.puts "Player 1: 0 and Player 2: 1"
end
def setplayer1
@player1 = 0
end
def setplayer2
@player2 = 1
end
def clearmatrix
@matrix = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
end
def getmatrixvalue(n)
@matrix[n]
end
def setmatrixvalue(i, v)
i = 1
v = "0"
@matrix[i] = "0"
end
def displaykey(matrix)
@matrix = ["1", "2", "3", "4", "5", "6", "7", "8", "9"]
@output.puts "Table key:\n|#{matrix[0]}|#{matrix[1]}|#{matrix[2]}|\n|#{matrix[3]}|#{matrix[4]}|#{matrix[5]}|\n|#{matrix[6]}|#{matrix[7]}|#{matrix[8]}|\n"
end
def displaymatrix
@matrix = ["_", "_", "_", "_", "_", "_", "_", "_", "_"]
@output.puts "Table status:\n|#{matrix[0]}|#{matrix[1]}|#{matrix[2]}|\n|#{matrix[3]}|#{matrix[4]}|#{matrix[5]}|\n|#{matrix[6]}|#{matrix[7]}|#{matrix[8]}|\n"
end
def finish
@output.puts "Finishing game..."
end
def displaymenu
@output.puts "Menu: (1)Start | (2)New | (9)Exit\n"
end
def checkwinner
end
在测试中,您正在手动设置单元格值。 在您的规范中,您可以设置
check_winner = "0"
在游戏类中,checkwinner
方法可以是这样的:
def checkwinner
if (matrix[0] == matrix[1]) && (matrix[1] == matrix[2])
matrix[0]
elsif # conditions for all other win cases
end
end
clearmatrix
方法只需在手动设置之前清除游戏区域。非常感谢,这真的很有帮助!