简体   繁体   中英

NoMethodError: undefined method `chomp' for nil:NilClass while running Ruby Rspec on gets.chomp

This is my first post here. I'm fairly new to Ruby, especially RSpec and have been running into an issue. I have written a method that uses gets.chomp to receive a player input. However I have this method called in another method

def prompt_move
        loop do     
            @move = gets.chomp.to_i 
            return move if valid_move?(move)                    
            puts "Invalid input. Enter a column number between 1 and 7"
        end
    end

    def valid_move?(move)
        @move.is_a?(Integer) && @move.between?(1, 7)
    end

    def play_round
        print_board
        prompt_player
        @move = prompt_move     
    end     

Here is the code for my RSpec tests:

describe ConnectFour do
  subject(:game) { described_class.new }
    let(:player){ double(Player) }

describe '#prompt_move' do      
        context 'move is a valid input' do          
            before do
                allow(game).to receive(:gets).and_return('3\n')
            end
            
            it 'returns move and stops the loop' do
                error_message = 'Invalid input. Enter a column number between 1 and 7'
                expect(game).to_not receive(:puts).with(error_message)              
                game.prompt_move
            end
        end
            
        context 'when given one invalid input, then a valid input' do
            before do
                letter = 'a'
                valid_input = '1'
                allow(game).to receive(:gets).and_return(letter, valid_input)
            end

            it 'completes loop and displays error message once' do                          
                error_message = 'Invalid input. Enter a column number between 1 and 7'              
                expect(game).to receive(:puts).with(error_message).once             
                game.prompt_move
            end
        end
    end

If I remove the #prompt_move method from #play_round the tests pass without any issue. However when I try to call it from within #play_round it gives me NoMethodError: undefined method `chomp' for nil:NilClass

I have been struggling to figure out what is causing this error so any suggestions would be greatly appreciated!

You're executing code in your class file.

new_game = ConnectFour.new
new_game.play_game

This will run every time you load the file, like when you're testing it. It will prompt for input and run gets . What it's getting is the code of the test file (for some rspec reason). That's invalid, so it keeps running gets until eventually there is no more input and gets returns nil .

Remove that from your class file. Code like that should be in a separate file which requires the class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM