Я хочу протестировать initialize
нового объекта, и в рамках этой инициализации есть вызов метода, который мне нужно смоделировать (этот метод просит пользователя ввести имя ... типичный случай)
class Setup
attr_reader :player
def initialize
@player = new_player(cli_input('the name'))
end
private
def cli_input('the name') # <<-- need to mock
$stdin.gets.chomp.strip
end
def new_player(name)
Player.new(name)
end
end
setup_spec.rb
RSpec.describe Battleship::Setup do
describe 'initialize' do
it 'creates a player assigned to a instance variable' do
allow_any_instance_of(Setup).to receive(:cli_input).with('the name').and_return('John')
setup = Battleship::Setup.new
expect(setup.player.name).to eq('John')
end
end
end
Это работает, но с использованием allow_any_instance_of
Как я могу проверить это без allow_any_instance_of
? , как я прочитал, его не следует использовать
Большое спасибо