0

I am new to rails and rspec, and currently taking an online a tutorial.

In the tutorial I am running the following code:

def display_board(board)
  puts " #{board[0]} | #{board[1]} | #{board[2]} "
  puts "-----------"
  puts " #{board[3]} | #{board[4]} | #{board[5]} "
  puts "-----------"
  puts " #{board[6]} | #{board[7]} | #{board[8]} "
end

board = [" "," "," "," "," "," "," "," "," "]
display_board(board)

When I run the test I get the following output:

/lib/display_board.rb
  defines a method display_board
  #display_board method
    represents a cell as a string with 3 spaces (FAILED - 1) 

Failures: 

  1) /lib/display_board.rb #display_board method represents a cell as a string with 3 spaces

     Failure/Error:
       def display_board(board)
         puts " #{board[0]} | #{board[1]} | #{board[2]} "
         puts "-----------"
         puts " #{board[3]} | #{board[4]} | #{board[5]} "
         puts "-----------"
         puts " #{board[6]} | #{board[7]} | #{board[8]} "
       end

     ArgumentError:
       wrong number of arguments (0 for 1)
     # ./lib/display_board.rb:2:in `display_board'
     # ./spec/display_board_spec.rb:10:in `block (4 levels) in <top (required)>' 
     # ./spec/spec_helper.rb:5:in `capture_puts'
     # ./spec/display_board_spec.rb:10:in `block (3 levels) in <top (required)>'

Finished in 0.00296 seconds (files took 0.14604 seconds to load)

2 examples, 1 failure

Failed examples:

rspec ./spec/display_board_spec.rb:9 # /lib/display_board.rb #display_board method represents a cell as a string with 3 spaces

The test case is as follows:

it 'represents a cell as a string with 3 spaces' do
  output = capture_puts{ display_board }
  expect(output).to include("   ")
end

Where line 10 is the spec file is "output = capture_puts{ display_board }"

And "capture_puts" is defined in the spec_helpr.rb as follows:

def capture_puts
  begin
    old_stdout = $stdout
    $stdout = StringIO.new('','w')
    yield
    $stdout.string
  ensure
    $stdout = old_stdout
  end
end

I searched for the error "wrong number of arguments (0 for 1)", but I didn't get any useful result. Please advice since I am really a beginner with Ruby and Rails.

2 Answers 2

1

You have to pass the board to display_board

board = [" "," "," "," "," "," "," "," "," "]
output = capture_puts{ display_board(board) }
expect(output).to include("   ")
Sign up to request clarification or add additional context in comments.

Comments

0

The error is due to this line

output = capture_puts{ display_board }

display_board requires an argument, so it should look like

output = capture_puts{ display_board(board) }

I also wanted to point out as well that there is an rspec matcher for testing stdout.

https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/output-matcher

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.