Я пытаюсь написать spe c, чтобы проверить, как мой код будет реагировать, когда пользователь просто нажимает клавишу «Ввод», т.е. не вводит никаких данных, просто нажимает «Ввод».
Сам код будет l oop, пока не будет введена правильная запись, но я не могу получить spe c для его проверки. Приведенный ниже код является примером как класса, так и объекта spe c.
. Обратите внимание, что в spe c я пытался заменить раздел «запрашивать повторно» на with_input (''), но это просто кажется зависает (или l oop)
class Example
def initialize(input: $stdin, output: $stdout)
@input = input
@output = output
end
def ask_for_number
@output.puts "Input an integer 5 or above"
loop do
input = @input.gets.to_i
return true if input >= 5
@output.puts "Invalid. Try again:"
end
end
end
--- И спец c
require 'stringio'
require_relative 'Example'
describe Example do
context 'with input greater than 5' do
it 'asks for input only once' do
output = ask_for_number_with_input(6)
expect(output).to eq "Input an integer 5 or above\n"
end
end
context 'with input equal to 5' do
it 'asks for input only once' do
output = ask_for_number_with_input('5')
expect(output).to eq "Input an integer 5 or above\n"
end
end
context 'with input less than 5' do
it 'asks repeatedly, until a number 5 or greater is provided' do
output = ask_for_number_with_input(2, 3, 6)
expect(output).to eq <<~OUTPUT
Input an integer 5 or above
Invalid. Try again:
Invalid. Try again:
OUTPUT
end
end
def ask_for_number_with_input(*input_numbers)
input = StringIO.new(input_numbers.join("\n"))
output = StringIO.new
example = Example.new(input: input, output: output)
expect(example.ask_for_number).to be true
output.string
end
end