I am trying to test if an instance variable saves input from user but I cant figure out how. I expect the test to pass but it fails and outputs expected "joe" got: "" to the terminal. Here is the method, I want to test:
def get_names
while Player.players < 2
while name.length <= 2
puts 'Each player must choose a name'
print 'Enter a name: '
@name = gets.chomp
end
unless name == ''
@player1 == '' ? @player1 = Player.new(name) : @player2 = Player.new(name)
name = ''
puts 'Name have been saved successfully'
end
end
end
And here is the rspec test suite:
describe Game do
subject(:game_names) { described_class.new }
describe '#get_names' do
context 'when a user inputs name' do
it 'saves in name instance variable' do
allow(game_names).to receive(:gets).and_return('joe')
name = game_names.instance_variable_get(:@name)
expect(name).to eq 'joe'
game_names.get_names
end
end
end
end
I gave tried mocking and stubbing the class, the method and the variable, but I can't get it to work. This is my first time writing tests.
expected "joe" got: ""yet I expected it to pass