【发布时间】:2015-05-17 05:28:55
【问题描述】:
# As a user, you can initialize the guessing game with a number, which is
the correct guess
# so the initialize method takes in one parameter, and sets game_complete?
to false
#
# As a user, I can guess the number, which will
# return :too_high if its > answer
# return :too_low if its < answer
# return :correct if its = answer
# correct changes the game_complete? to true
# if a user guesses the incorrect number after guessing the correct number,
it should
# change the game_complete? to false
# return :too_high or :too_low
require_relative 'guess'
describe GuessingGame do
let(:game) { GuessingGame.new(50) }
describe "#initialize" do
it "expects a single parameter" do
expect(GuessingGame.instance_method(:initialize).arity).to eq 1
end
end
describe "#guess" do
it "expects a single parameter" do
expect(GuessingGame.instance_method(:guess).arity).to eq 1
end
it "returns :too_low when the guess is lower than the answer" do
expect(game.guess(1)).to eq :too_low
end
it "returns :too_high when the guess is higher than the answer" do
expect(game.guess(100)).to eq :too_high
end
it "returns :correct when the guess matches answer" do
expect(game.guess(50)).to eq :correct
end
it "changes game_complete? when the correct guess is made" do
expect {
game.guess(50)
}.to change(game, :game_complete?).from(false).to(true)
end
it "doesn't change game_complete? when an incorrect guess is made" do
expect {
game.guess(10)
}.to_not change(game, :game_complete?).from(false)
end
it "returns :game_solved once you try to guess in a completed game" do
game.guess(50)
expect(game.guess(100)).to eq :game_solved
end
end
describe "#game_complete?" do
it "returns false in a new game" do
expect(game.game_complete?).to eq false
end
end
end
现在,当我运行此代码时,一旦您尝试在已完成的游戏中进行猜测,就会收到错误 GuessingGame#guess 返回:game_solved
这是我的猜测课
class GuessingGame
def initialize(num)
@num=num
def game_complete?
return false
end
end
def guess(num1)
if num1<@num
return :too_low
elsif num1>@num
return :too_high
else
def game_complete?
return true
end
return :correct
end
end
结束
我尝试用 false 初始化一个 bool 变量,一旦做出正确的猜测,我将其设置为 true,如果该变量为 true,我返回 :game_completed 但对我不起作用
【问题讨论】:
-
这不是初始化变量。这是定义方法。完全不同。您正在学习哪个教程/课程?
-
这不是在线课程,是学院培训课程
-
如果他们教你这个,你应该找另一个学院:)
标签: ruby-on-rails ruby rspec