所以我现在正在使用Learn.co gem,我正试图解决一个二十一点实验室。 https://github.com/learn-co-curriculum/simple-blackjack-cli 以下Rspec代码需要一个特定的输出,这使我感到困惑,因为我们使用随机数。这是Rspec代码:
describe "#runner" do
before(:each) do
def get_user_input
"h"
end
end
it "calls on the #welcome method,
then on the #initial_round method,
then calls #hit? and #display_card_total methods
-until- the card sum is greater than 21,
then calls on the #end_game method" do
expect(self).to receive(:deal_card).at_least(3).times.and_return(10)
expect(self).to receive(:get_user_input).and_return("h")
expect($stdout).to receive(:puts).with("Welcome to the Blackjack Table")
expect($stdout).to receive(:puts).with("Your cards add up to 20")
expect($stdout).to receive(:puts).with("Type 'h' to hit or 's' to stay")
expect($stdout).to receive(:puts).with("Your cards add up to 30")
expect($stdout).to receive(:puts).with("Sorry, you hit 30. Thanks for playing!")
runner
end
end

但是由于我们在以下代码中使用随机数,它如何才能成为Rspec的确切输出。这是Ruby代码:
def welcome
# code #welcome here
puts "Welcome to the Blackjack Table"
end
def deal_card
randomNumber = rand(1..11)
end
def display_card_total(total_cards)
puts "Your cards add up to #{total_cards}"
return total_cards
end
def prompt_user
puts "Type 'h' to hit or 's' to stay"
end
def get_user_input
letter = gets.chomp
end
def end_game(card_total)
puts "Sorry, you hit #{card_total}. Thanks for playing!"
end
def initial_round
initOne = deal_card()
initTwo = deal_card()
sumInit = initOne + initTwo
display_card_total(sumInit)
end
def hit?(myNumber)
prompt_user()
result = get_user_input()
card_total = myNumber
if result == 's'
return myNumber
elsif result == 'h'
sumInit = myNumber + deal_card()
return sumInit
else
invalid_command()
end
end
def invalid_command
puts "Please enter a valid command"
end
#####################################################
# get every test to pass before coding runner below #
#####################################################
def runner
welcome()
number = initial_round()
until number > 21
hit?(number)
display_card_total(number)
number += hit?(number)
end
end_game(number)
end

如果我们处理随机数,输出如何与Rspec测试相匹配?
答案 0 :(得分:0)
runner方法中的以下代码修复了问题。
def runner
welcome()
number = initial_round()
until number > 21
number = hit?(number)
display_card_total(number)
end
end_game(number)
end