Testing a complicated method
I'm working on a checkers implementation where I have dozens of easily tested methods, but I'm not sure how to test my main #play_game method. Where most of my methods have easily determined inputs and outputs, and are, therefore, easy to test, this method is multifaceted and really doesn't have an easily discernible output. Here is the code:
def play_game puts @gui.intro while(game_over? == false) message = nil @gui.render_board(@board) @gui.move_request player_input = gets coordinates = UserInput.translate_move_request_to_coordinates(player_input) message = MoveCheck.move_validator(coordinates, coordinates, coordinates, coordinates) puts message unless (message.nil? or message == "jumping move") if(message == nil or message == "jumping move") @current_player = switch_player unless (message == "jumping move" and jump_available? == true) end end puts @gui.display_game_ending_message end
So how can I test this (using RSpec) or should I not worry about it and really on my otherwise comprehensive testing?
All play_game is really doing is running the game loop. What you are really looking to test is what happens inside the game loop. The easiest way to do that is to break the content of the game loop down into more easily testable methods.
Once you have the game loop as just a series of methods, you can then much more easily test each of them in isolation.