Jules
Jules

Reputation: 303

Rspec failing test where 'Diff:', looks identical

Aim

My program uses an algorithm to make a board, line-by-line in an array of arrays.

The final array looks like this (a standard 3x3 board with a border):

[['-----'],['|...|'],['|...|'],['|...|'],['-----']]

Problem

The failing test is as follows:

Note: view_board puts @board

expect { board.view_board }.to output(['-----','|...|','|...|','|...|','-----']).to_stdout

Outputting:

 `Failure/Error: expect { board.view_board }.to output(['-----','|...|','|...|','|...|','-----']).to_stdout
   expected block to output ["-----", "|...|", "|...|", "|...|", "-----"] to stdout, but output "-----\n|...|\n|...|\n|...|\n-----\n"
   Diff:`

What'd different?

I'm not sure if there's a space somewhere I've missed, if I change the final array to "-----\n", then I get:

Diff: @@ -2,5 +2,5 @@ |...| |...| |...| ------\n +-----

Edit:

Board-Generating Code

class Board
BOARD_ROW = '-'
BOARD_COLUMM = '|'
BEGINNING_AND_END_LENGTH = 2

def initialize(board_size = 3)
    @board = []
    @board_top_and_bottom = []
    @board_middle = []
    @board_size = board_size
end

def go
    set_board
end

def set_board
    set_board_top_and_bottom
    set_board_middle
    assemble_board
    view_board
end

def set_board_top_and_bottom
    (@board_size + BEGINNING_AND_END_LENGTH).times do 
        @board_top_and_bottom.push(BOARD_ROW)    
    end
    @board_top_and_bottom = [@board_top_and_bottom.join]
end

def set_board_middle
    add_board_edge
    add_board_spaces
    add_board_edge
    @board_middle = [@board_middle.join]
end

def add_board_spaces
    @board_size.times do 
        @board_middle.push('.')    
    end
end

def add_board_edge
    @board_middle << BOARD_COLUMM
end

def assemble_board
    @board << @board_top_and_bottom
    @board_size.times do
        @board << @board_middle
    end
    @board << @board_top_and_bottom
end

def view_board
    puts @board
end
end

Upvotes: 1

Views: 2515

Answers (1)

spickermann
spickermann

Reputation: 106882

From the docs of puts:

puts(obj, ...) → nil Writes the given object(s) to ios. Writes a newline after any that do not already end with a newline sequence. [...]

That means when your call puts ['foo', 'bar'] then Ruby will actually output "foo\nbar"

To make your spec pass change the expectation to:

expect { board.view_board }.to output("-----\n|...|\n|...|\n|...|\n-----\n").to_stdout

Upvotes: 2

Related Questions