我是rails和rspec的新手,目前正在线上学习。
在本教程中,我运行以下代码:
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)
当我运行测试时,我得到以下输出:
/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
测试用例如下:
it 'represents a cell as a string with 3 spaces' do
output = capture_puts{ display_board }
expect(output).to include(" ")
end
第10行是规范文件是“output = capture_puts {display_board}”
“capture_puts”在spec_helpr.rb中定义如下:
def capture_puts
begin
old_stdout = $stdout
$stdout = StringIO.new('','w')
yield
$stdout.string
ensure
$stdout = old_stdout
end
end
我搜索了错误“错误的参数数量(0表示1)”,但我没有得到任何有用的结果。请建议,因为我真的是Ruby和Rails的初学者。
答案 0 :(得分:1)
您必须将董事会传递给display_board
board = [" "," "," "," "," "," "," "," "," "]
output = capture_puts{ display_board(board) }
expect(output).to include(" ")
答案 1 :(得分:0)
错误是由于这一行
output = capture_puts{ display_board }
display_board
需要一个参数,所以看起来应该是
output = capture_puts{ display_board(board) }
我还想指出,有一个用于测试stdout的rspec匹配器。
https://www.relishapp.com/rspec/rspec-expectations/docs/built-in-matchers/output-matcher